import { folderSchema, scriptTestRunSchema } from '../forms/schemas.js';
import {
  cloneScript,
  createFolder,
  createScript,
  deleteFolder,
  deleteScript,
  getScript,
  listFolders,
  listScripts,
  listScriptVersions,
  scriptUsage,
  updateFolder,
  updateScript
} from '../models/libraryModel.js';
import { camelJob } from '../models/mappers.js';
import { executeScriptTest } from '../services/powershellRunner.js';

export function folders(req, res) {
  res.json(listFolders(req.user.id));
}

export function createFolderAction(req, res) {
  const parsed = folderSchema.safeParse(req.body);
  if (!parsed.success) return res.status(400).json({ error: 'Invalid folder payload' });
  res.status(201).json(createFolder(parsed.data, req.user.id));
}

export function updateFolderAction(req, res) {
  const folder = updateFolder(req.params.id, req.body, req.user.id);
  if (!folder) return res.status(404).json({ error: 'Folder not found' });
  res.json(folder);
}

export function deleteFolderAction(req, res) {
  deleteFolder(req.params.id, req.user.id);
  res.status(204).end();
}

export function scripts(req, res) {
  res.json(listScripts(req.user.id));
}

export function createScriptAction(req, res) {
  // Model normalization creates the script row and initial version in one backend transaction path.
  res.status(201).json(createScript(req.body, req.user.id));
}

export function cloneScriptAction(req, res) {
  const script = cloneScript(req.params.id, req.body || {}, req.user.id);
  if (!script) return res.status(404).json({ error: 'Script not found' });
  res.status(201).json(script);
}

export function getScriptAction(req, res) {
  const script = getScript(req.params.id, req.user.id);
  if (!script) return res.status(404).json({ error: 'Script not found' });
  res.json(script);
}

export function updateScriptAction(req, res) {
  // Updating script content also appends a version record for audit and recovery.
  const script = updateScript(req.params.id, req.body, req.user.id);
  if (!script) return res.status(404).json({ error: 'Script not found' });
  res.json(script);
}

export function deleteScriptAction(req, res) {
  const usage = scriptUsage(req.params.id, req.user.id);
  if (!usage) return res.status(404).json({ error: 'Script not found' });
  if (usage.total && req.query.force !== 'true') {
    return res.status(409).json({
      error: 'Script is assigned to RunPlans or Intune deployments. Confirm deletion with force=true.',
      usage
    });
  }
  deleteScript(req.params.id, req.user.id);
  res.status(204).end();
}

export function scriptUsageAction(req, res) {
  const usage = scriptUsage(req.params.id, req.user.id);
  if (!usage) return res.status(404).json({ error: 'Script not found' });
  res.json(usage);
}

export function testRunScriptAction(req, res) {
  const parsed = scriptTestRunSchema.safeParse(req.body);
  if (!parsed.success) return res.status(400).json({ error: 'Invalid test run payload' });
  try {
    res.status(202).json(camelJob(executeScriptTest(req.params.id, parsed.data, req.user.id)));
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
}

export function scriptVersions(req, res) {
  const versions = listScriptVersions(req.params.id, req.user.id);
  if (!versions) return res.status(404).json({ error: 'Script not found' });
  res.json(versions);
}
