import { credentialSchema } from '../forms/schemas.js';
import { createCredential, deleteCredential, listCredentials, updateCredential } from '../models/credentialModel.js';

export function index(req, res) {
  res.json(listCredentials(req.user.id));
}

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

export function update(req, res) {
  const credential = updateCredential(req.params.id, req.body, req.user.id);
  if (!credential) return res.status(404).json({ error: 'Credential not found' });
  res.json(credential);
}

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