/**
 * Education Routes
 * Course, lesson, and enrollment management
 */

import { Router, RequestHandler } from 'express';
import * as educationController from './controllers/education.controller';
import { requireAuth } from '../../core/guards/auth.guard';

const router = Router();

// Type cast helper for authenticated routes
const authHandler = (fn: Function): RequestHandler => fn as RequestHandler;

// ============================================================================
// Public Routes
// ============================================================================

/**
 * GET /api/v1/education/categories
 * Get all course categories
 */
router.get('/categories', educationController.getCategories);

/**
 * GET /api/v1/education/courses
 * List published courses with filters and pagination
 * Query params: categoryId, level, isFree, search, minRating, page, pageSize, sortBy, sortOrder
 */
router.get('/courses', educationController.getCourses);

/**
 * GET /api/v1/education/courses/popular
 * Get popular courses
 * Query params: limit
 */
router.get('/courses/popular', educationController.getPopularCourses);

/**
 * GET /api/v1/education/courses/new
 * Get newest courses
 * Query params: limit
 */
router.get('/courses/new', educationController.getNewCourses);

/**
 * GET /api/v1/education/courses/:courseId
 * Get course by ID with full details
 */
router.get('/courses/:courseId', educationController.getCourseById);

/**
 * GET /api/v1/education/courses/slug/:slug
 * Get course by slug with full details
 */
router.get('/courses/slug/:slug', educationController.getCourseBySlug);

/**
 * GET /api/v1/education/courses/:courseId/modules
 * Get course modules with lessons
 */
router.get('/courses/:courseId/modules', educationController.getCourseModules);

/**
 * GET /api/v1/education/courses/:courseId/stats
 * Get course enrollment statistics
 */
router.get('/courses/:courseId/stats', educationController.getCourseStats);

/**
 * GET /api/v1/education/modules/:moduleId/lessons
 * Get module lessons
 */
router.get('/modules/:moduleId/lessons', educationController.getModuleLessons);

/**
 * GET /api/v1/education/lessons/:lessonId
 * Get lesson by ID
 */
router.get('/lessons/:lessonId', educationController.getLessonById);

// ============================================================================
// Authenticated User Routes
// ============================================================================

/**
 * GET /api/v1/education/my/enrollments
 * Get current user's course enrollments
 */
router.get('/my/enrollments', authHandler(requireAuth), authHandler(educationController.getMyEnrollments));

/**
 * GET /api/v1/education/my/stats
 * Get current user's learning statistics
 */
router.get('/my/stats', authHandler(requireAuth), authHandler(educationController.getMyLearningStats));

/**
 * POST /api/v1/education/courses/:courseId/enroll
 * Enroll in a free course
 */
router.post('/courses/:courseId/enroll', authHandler(requireAuth), authHandler(educationController.enrollInCourse));

/**
 * GET /api/v1/education/courses/:courseId/enrollment
 * Get enrollment status for a course
 */
router.get('/courses/:courseId/enrollment', authHandler(requireAuth), authHandler(educationController.getEnrollmentStatus));

/**
 * POST /api/v1/education/lessons/:lessonId/progress
 * Update lesson progress
 * Body: { videoWatchedSeconds?, videoCompleted?, userNotes? }
 */
router.post('/lessons/:lessonId/progress', authHandler(requireAuth), authHandler(educationController.updateLessonProgress));

/**
 * POST /api/v1/education/lessons/:lessonId/complete
 * Mark lesson as complete
 */
router.post('/lessons/:lessonId/complete', authHandler(requireAuth), authHandler(educationController.markLessonComplete));

// ============================================================================
// Instructor/Admin Routes (Course Management)
// ============================================================================

/**
 * POST /api/v1/education/categories
 * Create a new category (admin only)
 */
router.post('/categories', authHandler(requireAuth), authHandler(educationController.createCategory));

/**
 * POST /api/v1/education/courses
 * Create a new course (instructor/admin)
 */
router.post('/courses', authHandler(requireAuth), authHandler(educationController.createCourse));

/**
 * PATCH /api/v1/education/courses/:courseId
 * Update course details
 */
router.patch('/courses/:courseId', authHandler(requireAuth), authHandler(educationController.updateCourse));

/**
 * DELETE /api/v1/education/courses/:courseId
 * Delete a course
 */
router.delete('/courses/:courseId', authHandler(requireAuth), authHandler(educationController.deleteCourse));

/**
 * POST /api/v1/education/courses/:courseId/publish
 * Publish a course
 */
router.post('/courses/:courseId/publish', authHandler(requireAuth), authHandler(educationController.publishCourse));

/**
 * POST /api/v1/education/courses/:courseId/modules
 * Create a module in a course
 */
router.post('/courses/:courseId/modules', authHandler(requireAuth), authHandler(educationController.createModule));

/**
 * DELETE /api/v1/education/modules/:moduleId
 * Delete a module
 */
router.delete('/modules/:moduleId', authHandler(requireAuth), authHandler(educationController.deleteModule));

/**
 * POST /api/v1/education/modules/:moduleId/lessons
 * Create a lesson in a module
 */
router.post('/modules/:moduleId/lessons', authHandler(requireAuth), authHandler(educationController.createLesson));

/**
 * DELETE /api/v1/education/lessons/:lessonId
 * Delete a lesson
 */
router.delete('/lessons/:lessonId', authHandler(requireAuth), authHandler(educationController.deleteLesson));

export { router as educationRouter };
