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

const upload = require("../middlewares/upload.middleware");
const {
  submitAssignment,
  getSubmissionByAssignment,
  gradeSubmission,
  deleteSubmission,
  getSubmissionById,
  getStudentSubmissions
} = require("../controllers/submission.controller");
const submissionRouter = Router();

submissionRouter.post(
  "/submit/:assignmentId",
  authMiddleware,
  requireEnrollment({ allowTeacher: true }),
  authorizeRoles("student"),
  upload.single("file"),
  submitAssignment
);
submissionRouter.get(
  "/assignment/:assignmentId",
  authMiddleware,
  requireEnrollment({ allowTeacher: true }),
  authorizeRoles("teacher", "admin", "student"),
  getSubmissionByAssignment
);
submissionRouter.put(
  "/grade/:id",
  authMiddleware,
  authorizeRoles("teacher", "admin"),
  gradeSubmission
);
submissionRouter.delete(
  "/delete/:id",
  authMiddleware,
  authorizeRoles("admin"),
  deleteSubmission
);

submissionRouter.get("/my-submissions", authMiddleware, authorizeRoles("student"), getStudentSubmissions);


submissionRouter.get(
  "/:id",
  authMiddleware,
  authorizeRoles("teacher", "admin"),
  getSubmissionById
);


module.exports = submissionRouter;
