import { Router } from 'express';
import { getCurfew, updateCurfew, overrideCurfew } from '../services/curfewService.js';

const router = Router();

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

router.put('/', (req, res) => {
  try {
    const result = updateCurfew(req.body);
    res.json(result);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.post('/override', (req, res) => {
  try {
    const { minutes } = req.body;
    if (!minutes) return res.status(400).json({ error: '"minutes" is required' });
    const result = overrideCurfew(Number(minutes));
    res.json(result);
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

export default router;
