import { Router } from 'express';
import { getTimers, createTimer, cancelTimer } from '../services/timerService.js';

const router = Router();

router.get('/', (req, res) => {
  try {
    res.json(getTimers());
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.post('/', async (req, res) => {
  try {
    const { category, minutes } = req.body;
    if (!category || !minutes) {
      return res.status(400).json({ error: '"category" and "minutes" are required' });
    }
    const timer = await createTimer(category, Number(minutes));
    res.json(timer);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.delete('/:id', (req, res) => {
  try {
    const timer = cancelTimer(req.params.id);
    res.json({ cancelled: true, timer });
  } catch (err) {
    res.status(400).json({ error: err.message });
  }
});

export default router;
