const express = require("express");
const router = express.Router();
const authenticate = require("../../middlewares/authenticate");
const authorize = require("../../middlewares/authorize");
const dashboardController = require("../../controllers/mobile/dashboardController");
const upload = require("../../middlewares/upload")

/**
 * @swagger
 * tags:
 *   name: Mobile Dashboard 
 *   description: Mobile (Field Executive / Agent) APIs
 */

router.get(
  "/dashboard",
  authenticate,
  authorize(["agent"]),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Get mobile dashboard data for field executive'
      #swagger.description = 'Returns task counts, TAT alerts, leaderboard rank and summary stats for logged-in agent'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.responses[200] = {
        description: 'Dashboard data fetched successfully',
        schema: {
          success: true,
          data: {
            tasks: 50,
            completed: 5,
            cancelled: 3,
            tatAlerts: 2,
            leaderboard: {
              rank: 48,
              name: "Satish"
            }
          }
        }
      }

      #swagger.responses[401] = {
        description: 'Unauthorized'
      }

      #swagger.responses[403] = {
        description: 'Access denied'
      }
    */
    dashboardController.getMobileDashboard(req, res);
  }
);


router.get(
  "/tasks",
  authenticate,
  authorize(["agent"]),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Get all tasks assigned to the agent (mobile)'
      #swagger.security = [{ "bearerAuth": [] }]
    */
    dashboardController.getAgentTasksMobile(req, res);
  }
);

router.post(
  "/tasks/:id/accept",
  authenticate,
  authorize(["agent"]),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Accept assigned task'
      #swagger.description = 'Agent accepts a task assigned by admin'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Task ID'
      }
      #swagger.responses[200] = {
        description: 'Task accepted successfully'
      }
    */
    dashboardController.acceptTask(req, res);
  }
);

router.post(
  "/tasks/:id/reject",
  authenticate,
  authorize(["agent"]),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Reject assigned task'
      #swagger.description = 'Agent rejects a task assigned by admin with a reason'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Task ID'
      }
      #swagger.responses[200] = {
        description: 'Task rejected successfully'
      }
    */
    dashboardController.rejectTask(req, res);
  }
);


router.post(
  "/tasks/:id/start",
  authenticate,
  authorize(["agent"]),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Start task work'
      #swagger.description = 'Agent starts working on the task with location capture'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Task ID'
      }
      #swagger.parameters['body'] = {
        in: 'body',
        required: false,
        schema: {
          lat: 17.385044,
          lng: 78.486671
        }
      }
      #swagger.responses[200] = {
        description: 'Task started successfully'
      }
    */
    dashboardController.startTask(req, res);
  }
);


router.post(
  "/tasks/:id/complete",
  authenticate,
  authorize(["agent"]),
  upload.array("images", 5),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Complete task work'
      #swagger.description = 'Agent completes the task with end location capture'
      #swagger.security = [{ "bearerAuth": [] }]

      #swagger.consumes = ['multipart/form-data']

      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Task ID'
      }

      #swagger.parameters['images'] = {
        in: 'formData',
        type: 'array',
        items: { type: 'file' },
        description: 'Completion images (max 5)'
      }

      #swagger.parameters['lat'] = {
        in: 'formData',
        type: 'number'
      }

      #swagger.parameters['lng'] = {
        in: 'formData',
        type: 'number'
      }

      #swagger.parameters['description'] = {
        in: 'formData',
        type: 'string'
      }

      #swagger.responses[200] = {
        description: 'Task completed successfully'
      }
    */
    dashboardController.completeTask(req, res);
  }
);


router.post(
  "/tasks/:id/issue",
  authenticate,
  authorize(["agent"]),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Raise issue on task'
      #swagger.description = 'Agent raises an issue during task execution'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Task ID'
      }
    */
    dashboardController.raiseIssue(req, res);
  }
);


router.get(
  "/tasks/tat-alerts",
  authenticate,
  authorize(["agent"]),
  (req, res) => {
    /*
      #swagger.tags = ['Mobile Agent Tasks']
      #swagger.summary = 'Get TAT alerts'
      #swagger.description = 'Get overdue tasks based on due date'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.responses[200] = {
        description: 'TAT alerts fetched successfully'
      }
    */
    dashboardController.getAgentTatAlerts(req, res);
  }
);


// router.post(
//   "/customers",
//   authenticate,
//   authorize(["admin", "superadmin", "manager", "agent"]),
//   (req, res) => {
//     /*
//       #swagger.tags = ['Mobile Agent Tasks']
//       #swagger.summary = 'Create customer by agent'
//       #swagger.security = [{ "bearerAuth": [] }]

//       #swagger.requestBody = {
//         required: true,
//         content: {
//           "application/json": {
//             example: {
//               name: "ABC Enterprises",
//               customerNumber: "CUST-001",
//               email: "contact@abc.com",
//               phone: "9876543210",
//               address: "Plot 21, Madhapur, Hyderabad",
//               state: "Telangana",
//               city: "Hyderabad",
//               primaryContactPerson: "Ramesh Kumar",
//               notes: "Preferred corporate customer"
//             }
//           }
//         }
//       }
//     */
//     dashboardController.createCustomer(req, res);
//   }
// );



module.exports = router;
