import { customVariableSchema } from '../forms/schemas.js';
import {
  createCustomVariable,
  deleteCustomVariable,
  listCustomVariables,
  listVariableCatalog,
  updateCustomVariable
} from '../models/variableModel.js';

export function catalog(req, res) {
  res.json(listVariableCatalog(req.user.id));
}

export function customIndex(req, res) {
  res.json(listCustomVariables(req.user.id));
}

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

export function updateCustom(req, res) {
  const variable = updateCustomVariable(req.params.id, req.body, req.user.id);
  if (!variable) return res.status(404).json({ error: 'Variable not found' });
  res.json(variable);
}

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