import { Router } from 'express';
import { createAuditEvent, listSchedules, replaceSchedules } from '../store.js';

export const schedulesRouter = Router();

schedulesRouter.get('/', (_req, res) => {
  res.json(listSchedules());
});

schedulesRouter.put('/', (req, res, next) => {
  try {
    const schedules = replaceSchedules(req.body?.schedules || []);
    createAuditEvent({ user: req.user, action: 'replace', targetType: 'schedules', details: { count: schedules.length } });
    res.json(schedules);
  } catch (error) {
    next(error);
  }
});
