const { Router } = require("express");
const { authMiddleware } = require("../middlewares/auth.middleware");
const { authorizeRoles } = require("../middlewares/role.middleware");
const upload = require("../middlewares/upload.middleware");
const { requireEnrollment } = require("../middlewares/enrollment.middleware");

const {
  createLesson,
  getSubjectLessons,
  deleteLesson,
  getLessonById,
  updateLessonProgress,
  getLessonsWithProgress,
} = require("../controllers/lesson.controller");

const lessonRouter = Router();

// -------------------- LESSON CRUD -------------------- //

// Create lesson (teacher only)
lessonRouter.post(
  "/create/:subjectId",
  authMiddleware,
  authorizeRoles("teacher"),
  upload.single("file"),
  createLesson
);

// Get all lessons of a subject (any logged-in user, enrollment required)
lessonRouter.get(
  "/:subjectId",
  authMiddleware,
  requireEnrollment({ allowTeacher: true }),
  getSubjectLessons
);

// Get single lesson by ID (any logged-in user, enrollment required)
lessonRouter.get(
  "/lesson/:id",
  authMiddleware,
  requireEnrollment({ allowTeacher: true }),
  getLessonById
);

// Delete lesson (teacher/admin only)
lessonRouter.delete(
  "/delete/:id",
  authMiddleware,
  authorizeRoles("teacher", "admin"),
  deleteLesson
);

// -------------------- LESSON PROGRESS -------------------- //

// Update lesson progress (student only)
lessonRouter.put(
  "/progress/:lessonId",
    authMiddleware,
  requireEnrollment({ allowTeacher: true }),
  updateLessonProgress
);

// Get all lessons with progress for a subject (student only)
lessonRouter.get(
  "/subject/:subjectId/progress",
    authMiddleware,
  requireEnrollment({ allowTeacher: true }),
  getLessonsWithProgress
);

module.exports = lessonRouter;
