/**
 * Exercise Routes
 *
 * Route definitions for exercise endpoints
 */

import { Router } from 'express';
import { exerciseController } from './exercise.controller';
import { authenticate } from '../../shared/middleware/auth.middleware';

const router = Router();

// All exercise routes require authentication
router.use(authenticate);

/**
 * @route   GET /api/exercises
 * @desc    List exercises with optional filtering
 * @query   moduleId - Filter by module ID
 * @query   topicId - Filter by topic ID
 * @query   type - Filter by exercise type (MULTIPLE_CHOICE, OPEN_RESPONSE, CALCULATION, PROOF, TRUE_FALSE)
 * @query   difficulty - Filter by difficulty (BASIC, INTERMEDIATE, ADVANCED, EXPERT)
 * @query   isPublished - Filter by published status (default: true)
 * @query   page - Page number (default: 1)
 * @query   limit - Items per page (default: 50)
 * @query   sortBy - Sort field (default: order)
 * @query   sortOrder - Sort direction asc/desc (default: asc)
 * @access  Private
 */
router.get('/', exerciseController.listExercises.bind(exerciseController));

/**
 * @route   GET /api/exercises/:id
 * @desc    Get exercise by ID
 * @param   id - Exercise ID
 * @query   includeModule - Include module info (default: false)
 * @query   includeTopic - Include topic info (default: false)
 * @query   includeUserAttempts - Include user's attempts (default: false)
 * @query   hideSolution - Hide solution (default: true)
 * @access  Private
 */
router.get(
  '/:id',
  exerciseController.getExerciseById.bind(exerciseController)
);

/**
 * @route   POST /api/exercises/:id/attempt
 * @desc    Submit an exercise attempt
 * @param   id - Exercise ID
 * @body    userAnswer - User's answer (required if not skipped)
 * @body    timeSpentSeconds - Time spent on exercise (required)
 * @body    hintsUsed - Number of hints used (default: 0)
 * @body    skipped - Whether exercise was skipped (default: false)
 * @access  Private
 */
router.post(
  '/:id/attempt',
  exerciseController.submitAttempt.bind(exerciseController)
);

/**
 * @route   GET /api/exercises/:id/solution
 * @desc    Get exercise solution (unlocked after completion)
 * @param   id - Exercise ID
 * @access  Private
 */
router.get(
  '/:id/solution',
  exerciseController.getExerciseSolution.bind(exerciseController)
);

/**
 * @route   GET /api/exercises/:id/hints
 * @desc    Get exercise hints
 * @param   id - Exercise ID
 * @access  Private
 */
router.get(
  '/:id/hints',
  exerciseController.getExerciseHints.bind(exerciseController)
);

/**
 * @route   GET /api/exercises/:id/attempts
 * @desc    Get user's attempts for an exercise with pagination
 * @param   id - Exercise ID
 * @query   page - Page number (default: 1)
 * @query   limit - Items per page (default: 10, max: 100)
 * @access  Private
 * @returns {
 *   success: boolean,
 *   data: Array<ExerciseAttempt>,
 *   meta: {
 *     totalAttempts: number,
 *     bestScore: number,
 *     hasCompleted: boolean,
 *     pagination: { page, limit, total, totalPages, hasNext, hasPrev }
 *   }
 * }
 */
router.get(
  '/:id/attempts',
  exerciseController.getUserAttempts.bind(exerciseController)
);

/**
 * @route   GET /api/exercises/:id/next
 * @desc    Get next exercise in sequence
 * @param   id - Current exercise ID
 * @access  Private
 */
router.get(
  '/:id/next',
  exerciseController.getNextExercise.bind(exerciseController)
);

/**
 * @route   GET /api/module/:moduleId/practice
 * @desc    Get exercises for practice mode
 * @param   moduleId - Module ID
 * @query   difficulty - Filter by difficulty (optional)
 * @query   count - Number of exercises to return (default: 10)
 * @query   excludeCompleted - Exclude completed exercises (default: false)
 * @access  Private
 */
router.get(
  '/module/:moduleId/practice',
  exerciseController.getPracticeExercises.bind(exerciseController)
);

export default router;
