// routes/taskRoutes.js
const express = require("express");
const router = express.Router();
const taskController = require("../controllers/taskController");
const authenticate = require("../middlewares/authenticate");
const authorize = require("../middlewares/authorize");
const upload = require("../middlewares/upload");
const uploadExcel = require("../middlewares/uploadExcel")


/**
 * @swagger
 * tags:
 *   name: Tasks
 *   description: Task management APIs
 */

// CREATE task (manager)
router.post("/create", authenticate, authorize(["admin", "superadmin", "manager"]), upload.array("attachments"), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Create a new task'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Task details including customer info and orgId',
      required: true,
      schema: {
        taskName: "Install Router",
        description: "Install router at client site",
        mobile: "9876543210",
        dueDate: "2025-09-10T10:00:00Z",
        status: "Pending",
        priority: "High",
        sla: "24 hours",
        assignedTo: "John Doe",
        assignedBy: "Admin",
        location: "Hyderabad",
        customer: { name: "ABC Corp", email: "abc@example.com", phone: "9876543210" },
        orgId: "64f5a2d8c9e4b3a1f0a12345"
      }
    }
  */
  taskController.createTask(req, res);
});



// my post api for create task one route
// router.post("/createOne", authenticate,authorize(["admin", "agent", "manager"]), upload.array("attachments"), (req,res)=> {
//   /*
//     #swagger.tags = ['Tasks']
//     #swagger.summary = 'Create a new task - test'
//   */
//   taskController.createTaskOne(req, res);
// });
// router.get("/getOne/:id", authenticate, (req,res)=> {
//   /*
//     #swagger.tags = ['Tasks']
//     #swagger.summary = 'Get a task by ID - test'
//   */
//   taskController.getTaskByIdOne(req, res);
// })





// ------------------ CREATE NEXT RECURRING TASK ------------------


router.post("/recurring/next/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Create the next occurrence of a recurring task'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
  */
  taskController.createNextRecurringTask(req, res);
});

// GET all tasks (manager)
router.get("/get/all", authenticate, authorize(["admin", "manager",]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Get all tasks with pagination'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['page'] = { description: 'Page number', in: 'query' }
    #swagger.parameters['limit'] = { description: 'Number of tasks per page', in: 'query' }
  */
  taskController.getTasks(req, res);
});

// ------------------ GET TASKS BY ASSIGNEE ------------------
router.get("/assignee/:userId", authenticate, authorize(["admin", "manager", "agent"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Get tasks assigned to a specific user'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['userId'] = { description: 'User ID', required: true }
  */
  taskController.getTasksByAssignee(req, res);
});

// ------------------ GET TASKS BY ORG ------------------
router.get("/org/:orgId", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Get all tasks for an organization'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['orgId'] = { description: 'Organization ID', required: false }
  */
  taskController.getTasksByOrg(req, res);
});

// ------------------ GET OVERDUE TASKS ------------------
router.get("/overdue", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Get all overdue tasks'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  taskController.getOverdueTasks(req, res);
});

// GET task by ID (all logged-in users)
router.get("/get/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Get a task by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
  */
  taskController.getTaskById(req, res);
});

// UPDATE task (manager)
router.put("/update/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Update a task by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Update fields for task',
      required: true,
      schema: { status: "In Progress", priority: "Medium", assignedTo: "Jane Doe" }
    }
  */
  req.body.orgId = req.body.orgId || req.user?.orgId;
  taskController.updateTask(req, res);
});

// ------------------ ASSIGN TASK ------------------
router.put("/assign/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Assign a task to a user'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Assignee user ID',
      required: true,
      schema: { assignee: "UserObjectId" }
    }
  */
  taskController.assignTask(req, res);
});

// ------------------ REASSIGN TASK ------------------
router.put("/reassign/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Reassign a task to a different user'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'New assignee details',
      required: true,
      schema: { newAssignee: "UserObjectId" }
    }
  */
  taskController.reassignTask(req, res);
});

// ------------------ REVOKE TASK ------------------
router.put("/revoke/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Revoke a task from its current assignee'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
  */
  taskController.revokeTask(req, res);
});




// ------------------ ADD ATTACHMENT ------------------
router.put("/attachment/:id", authenticate, authorize(["admin", "manager", "agent"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Add an attachment to a task'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Attachment details',
      required: true,
      schema: { fileName: "report.pdf", fileUrl: "https://example.com/report.pdf" }
    }
  */
  taskController.addAttachment(req, res);
});

// ------------------ ADD COMMENT ------------------
router.put("/comment/:id", authenticate, authorize(["admin", "manager", "agent"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Add a comment to a task'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
    #swagger.parameters['body'] = {
      in: 'body',
      description: 'Comment text',
      required: true,
      schema: { text: "This task needs urgent review." }
    }
  */
  taskController.addComment(req, res);
});




// DELETE task (manager)
router.delete("/delete/:id", authenticate, authorize(["admin", "manager"]), (req, res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'Soft delete a task by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
  */
  taskController.deleteTask(req, res);
});


// get Agent task assignTo
router.get("/assign/:id", authenticate, authorize(["admin", "manager"]), (req,res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'get a Agenttask by ID'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['id'] = { description: 'Task ID', required: true }
  */
  taskController.getAgentTask(req,res)
})

router.get("/stats", authenticate, authorize(["admin", "manager"]), (req,res) => {
  /*
    #swagger.tags = ['Tasks']
    #swagger.summary = 'get task statistics'
    #swagger.security = [{ "bearerAuth": [] }]
  */
  taskController.getTaskStats(req,res)
})


//  Download task Excel template
router.get(
  "/template",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Tasks']
      #swagger.summary = 'Download Excel template for bulk task upload'
      #swagger.description = 'Downloads a predefined Excel template used for bulk task creation'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    taskController.downloadTaskTemplate(req, res);
  }
);

//  Bulk upload tasks via Excel
router.post(
  "/bulk-upload",
  authenticate,
  authorize(["admin", "manager"]),
  uploadExcel.single("file"),
  (req, res) => {
    /*
      #swagger.tags = ['Tasks']
      #swagger.summary = 'Bulk upload tasks using Excel file'
      #swagger.description = 'Uploads an Excel file and creates multiple tasks in a single request'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['file'] = {
        in: 'formData',
        type: 'file',
        required: true,
        description: 'Excel file (.xlsx or .xls) containing task data'
      }
    */
    taskController.bulkUploadTasks(req, res);
  }
);

router.get("/export",authenticate, authorize(["admin", "manager"]),  (req,res) => {

   /*
      #swagger.tags = ['Tasks']
      #swagger.summary = 'Bulk export tasks using Excel file'
      #swagger.description = 'Uploads an Excel file and creates multiple tasks in a single request'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.consumes = ['multipart/form-data']
      #swagger.parameters['file'] = {
        in: 'formData',
        type: 'file',
        required: true,
        description: 'Excel file (.xlsx or .xls) containing task data'
      }
    */

  taskController.exportTasksExcel(req, res)

}
  
);




module.exports = router;
