import { hostSchema } from '../forms/schemas.js';
import { createHost, deleteHost, listHosts, updateHost } from '../models/hostModel.js';

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

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

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

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