import { Router } from 'express';
import { ticketService } from '../services/TicketService.js';

const router = Router();

router.get('/', (_req, res) => {
  res.json(ticketService.getAll());
});

router.get('/:id', (req, res) => {
  const ticket = ticketService.getDetail(req.params.id);
  if (!ticket) {
    res.status(404).json({ error: 'Ticket not found' });
    return;
  }

  res.json(ticket);
});

router.post('/:id/complete', async (req, res) => {
  const branchId = req.body?.branchId ?? null;
  if (branchId !== null && typeof branchId !== 'string') {
    res.status(400).json({ error: 'branchId must be a string when provided' });
    return;
  }

  const result = await ticketService.markComplete(req.params.id, {
    branchId
  });
  res.json(result);
});

export default router;
