import express from 'express';
import { authenticate } from '../middleware/authMiddleware.js';
import schedulerService from '../services/schedulerService.js';

const router = express.Router();

router.post('/rating_updates', authenticate, async (req, res) => {
  try {
    const result = await schedulerService.triggerRatingUpdates();
    res.json(result);
  } catch (error) {
    res.status(500).json({ success: false, message: error.message });
  }
});

router.post('/match_results', authenticate, async (req, res) => {
  try {
    const result = await schedulerService.triggerMatchResultsFetch({
      userId: req.user.id,
      clubTeamId: req.body?.clubTeamId ?? null,
      currentSeasonOnly: true
    });
    res.json(result);
  } catch (error) {
    res.status(500).json({ success: false, message: error.message });
  }
});

router.get('/status', authenticate, (req, res) => {
  const status = schedulerService.getStatus();
  const nextRatingUpdate = schedulerService.getNextRatingUpdateTime();
  res.json({
    ...status,
    nextRatingUpdate
  });
});

export default router;
