import { Router } from "express";
import { Role } from "@prisma/client";
import { requireAuth, requireRole } from "../../middleware/auth";
import { todoController } from "./todos.controller";

const router = Router();

// All todo endpoints require authentication
router.use(requireAuth);

// Create todo
router.post("/", requireRole([Role.ADMIN, Role.MANAGER]), (req, res) => todoController.createTodo(req, res));

// List todos with filters/sort/pagination + role-based visibility
router.get("/", requireRole([Role.ADMIN, Role.MANAGER]), (req, res) => todoController.listTodos(req, res));

// Get todos for a specific user (role-based visibility)
router.get("/user/:userId", requireRole([Role.ADMIN, Role.MANAGER]), (req, res) => todoController.getUserTodos(req, res));

// Update todo
router.put("/:id", requireRole([Role.ADMIN, Role.MANAGER]), (req, res) => todoController.updateTodo(req, res));

// Delete todo
router.delete("/:id", requireRole([Role.ADMIN, Role.MANAGER]), (req, res) => todoController.deleteTodo(req, res));

// Comments: list
router.get(
  "/:id/comments",
  requireRole([Role.ADMIN, Role.MANAGER]),
  (req, res) => todoController.listComments(req, res)
);

// Comments: add
router.post(
  "/:id/comments",
  requireRole([Role.ADMIN, Role.MANAGER]),
  (req, res) => todoController.addComment(req, res)
);

// Comments: update (author only)
router.put(
  "/:todoId/comments/:commentId",
  requireRole([Role.ADMIN, Role.MANAGER]),
  (req, res) => todoController.updateComment(req, res)
);

// Comments: delete (author only)
router.delete(
  "/:todoId/comments/:commentId",
  requireRole([Role.ADMIN, Role.MANAGER]),
  (req, res) => todoController.deleteComment(req, res)
);

export default router;
