const router = require('express').Router();
const { authMiddleware } = require('../middleware/auth');
const rateLimit = require('../middleware/rateLimit');
const validate  = require('../middleware/validate');
const { start, answer, finish, result, history, weakTopics, getSessionQuestions, stats } = require('../controllers/sessionController');

const sessionLimiter = rateLimit({ windowMs: 60_000, max: 10, message: 'Слишком много сессий, подождите минуту' });
const startSchema = { body: {
  subject_slug: { type: 'string', required: true, minLen: 1, maxLen: 100 },
  mode:  { type: 'string', oneOf: ['exam', 'practice', 'repeat', 'ct'] },
  count: { type: 'number', min: 1, max: 200, integer: true },
}};
const answerSchema = { body: {
  question_id: { type: 'number', required: true, min: 1, integer: true },
}};

router.use(authMiddleware); // все маршруты требуют авторизации

router.post('/', sessionLimiter, validate(startSchema), start);
router.post('/:id/answer', validate(answerSchema), answer);
router.post('/:id/finish',    finish);
router.get('/history',        history);
router.get('/stats',          stats);
router.get('/weak-topics',    weakTopics);
router.get('/:id/result',     result);
router.get('/:id/questions',  getSessionQuestions);

module.exports = router;
