/**
 * Task Routes
 */

const express = require("express");
const router = express.Router();
const { body } = require("express-validator");
const { validate } = require("../middleware/validationMiddleware");
const { authenticate, authorize } = require("../middleware/authMiddleware");
const taskController = require("../controllers/taskController");
const commentController = require("../controllers/commentController");

// Validation rules
const createTaskValidation = [
  body("title").notEmpty().withMessage("Task title is required"),
  body("priority")
    .optional()
    .isIn(["Low", "Medium", "High"])
    .withMessage("Invalid priority"),
  body("dueDate").optional().isDate().withMessage("Valid due date is required"),
];

const updateStatusValidation = [
  body("status")
    .isIn(["Pending", "In Progress", "Completed", "On Hold"])
    .withMessage("Invalid status"),
];

// Apply authentication to all routes
router.use(authenticate);

// Statistics and special routes
router.get("/my-tasks", taskController.getMyTasks);
router.get("/statistics", taskController.getTaskStatistics);
router.get(
  "/overdue",
  authorize("Admin", "Manager"),
  taskController.getOverdueTasks,
);
router.get("/upcoming", taskController.getUpcomingDeadlines);

// CRUD routes
router.get("/", taskController.getAllTasks);
router.get("/:id", taskController.getTaskById);
router.post(
  "/",
  authorize("Admin", "Manager"),
  createTaskValidation,
  validate,
  taskController.createTask,
);
router.put("/:id", authorize("Admin", "Manager"), taskController.updateTask);
router.patch(
  "/:id/status",
  updateStatusValidation,
  validate,
  taskController.updateTaskStatus,
);
router.delete("/:id", authorize("Admin", "Manager"), taskController.deleteTask);

// Comment routes for tasks
router.get("/:taskId/comments", commentController.getCommentsByTask);
router.post("/:taskId/comments", commentController.createComment);

module.exports = router;
