
const express = require("express");
const router = express.Router();

const shiftPlannerController = require("../../controllers/hrandpayrollsController/ShiftPlannerController");
const authenticate = require("../../middlewares/authenticate");



// Make sure this is AFTER the authentication middleware
router.post("/save-pattern", authenticate, (req, res) => {
  /*  #swagger.tags = ['ShiftPlanner']
  #swagger.summary = 'Save a repeating shift pattern'
  #swagger.security = [{ "bearerAuth": [] }]
  #swagger.requestBody = {
    required: true,
    content: {
      "application/json": {
        schema: {
          type: "object",
          properties: {
            plan_id: { type: "string" },
            shift_id: { type: "string" },
            day_of_week: { type: "integer" },
            start_time: { type: "string", format: "time" },
            end_time: { type: "string", format: "time" }
          },
          required: ["plan_id", "shift_id", "day_of_week", "start_time", "end_time"]
        }
      }
    }
  }
  */

  shiftPlannerController.saveRepeatingPattern(req, res);
});
/**
 * @swagger
 * tags:
 *   name: ShiftPlanner
 *   description: Shift planning and group management APIs
 */

/* ===========================
   SPECIFIC ROUTES (NO PARAMETERS) - MUST COME FIRST
=========================== */

/**
 * @swagger
 * /shift-planner/plans/stats:
 *   get:
 *     tags: [ShiftPlanner]
 *     summary: Get shift plans statistics
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Statistics retrieved successfully
 */
router.get("/plans/stats", authenticate, (req, res) => {
  shiftPlannerController.getShiftPlansStats(req, res);
});

/**
 * @swagger
 * /shift-planner/plans:
 *   get:
 *     tags: [ShiftPlanner]
 *     summary: Get all shift plans
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema: { type: integer, default: 1 }
 *       - in: query
 *         name: limit
 *         schema: { type: integer, default: 10 }
 *     responses:
 *       200:
 *         description: List of shift plans
 */
router.get("/plans", authenticate, (req, res) => {
  shiftPlannerController.getShiftPlans(req, res);
});

/**
 * @swagger
 * /shift-planner/groups:
 *   get:
 *     tags: [ShiftPlanner]
 *     summary: Get all groups
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: List of groups with members
 */
router.get("/groups", authenticate, (req, res) => {
  shiftPlannerController.getGroups(req, res);
});

// Group lookup routes
router.get('/shift-planner/group-for-shift-date', authenticate, (req, res) => {
  /*  #swagger.tags = ['ShiftPlanner']
  #swagger.summary = 'Get group assigned to a specific shift and date'
  #swagger.security = [{ "bearerAuth": [] }]
  */
  shiftPlannerController.getGroupForShiftDate(req, res);
});

router.get('/shift-planner/group-by-shift-date', authenticate, (req, res) => {
  /*  #swagger.tags = ['ShiftPlanner']
  #swagger.summary = 'Get group for a specific shift and date'
  #swagger.security = [{ "bearerAuth": [] }]
  */
  shiftPlannerController.getGroupByShiftAndDate(req, res);
});

/**
 * @swagger
 * /shift-planner/groups/all:
 *   get:
 *     tags: [ShiftPlanner]
 *     summary: Get groups for a plan
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: plan_id
 *         schema: { type: string }
 *     responses:
 *       200:
 *         description: Groups for plan retrieved
 */
router.get("/groups/all", authenticate, (req, res) => {
  shiftPlannerController.getGroupsForPlan(req, res);
});

/**
 * @swagger
 * /shift-planner/cells:
 *   get:
 *     tags: [ShiftPlanner]
 *     summary: Get plan cells
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: plan_id
 *         schema: { type: string }
 *       - in: query
 *         name: shift_id
 *         schema: { type: string }
 *       - in: query
 *         name: date
 *         schema: { type: string }
 *     responses:
 *       200:
 *         description: Plan cells retrieved
 */
router.get("/cells", authenticate, (req, res) => {
  shiftPlannerController.getPlanCells(req, res);
});

/**
 * @swagger
 * /shift-planner/cells-with-groups:
 *   get:
 *     tags: [ShiftPlanner]
 *     summary: Get plan cells with group details
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: Cells with groups retrieved
 */
router.get("/cells-with-groups", authenticate, (req, res) => {
  shiftPlannerController.getShiftPlanCellsWithGroups(req, res);
});

/**
 * @swagger
 * /shift-planner/save:
 *   post:
 *     tags: [ShiftPlanner]
 *     summary: Save shift planner
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       201:
 *         description: Shift planner saved successfully
 */
router.post("/save", authenticate, (req, res) => {
  shiftPlannerController.saveShiftPlanner(req, res);
});



/**
 * @swagger
 * /shift-planner/groups:
 *   post:
 *     tags: [ShiftPlanner]
 *     summary: Create a new group
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       201:
 *         description: Group created successfully
 */
router.post("/groups", authenticate, (req, res) => {
  shiftPlannerController.createGroup(req, res);
});

/**
 * @swagger
 * /shift-planner/plan/{plan_id}/cells:
 *   post:
 *     tags: [ShiftPlanner]
 *     summary: Save plan cells
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: plan_id
 *         required: true
 *         schema: { type: string }
 *     responses:
 *       201:
 *         description: Cells saved successfully
 */
router.post("/plan/:plan_id/cells", authenticate, (req, res) => {
  shiftPlannerController.savePlanCells(req, res);
});

/* ===========================
   ROUTES WITH PARAMETERS - COME AFTER SPECIFIC ROUTES
=========================== */

/**
 * @swagger
 * /shift-planner/plans/{id}:
 *   get:
 *     tags: [ShiftPlanner]
 *     summary: Get shift plan by ID
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: string }
 *     responses:
 *       200:
 *         description: Plan details retrieved
 *       404:
 *         description: Plan not found
 */
router.get("/plans/:id", authenticate, (req, res) => {
  shiftPlannerController.getShiftPlanById(req, res);
});

/**
 * @swagger
 * /shift-planner/plans/{id}:
 *   put:
 *     tags: [ShiftPlanner]
 *     summary: Update shift plan
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: string }
 *     responses:
 *       200:
 *         description: Plan updated successfully
 */
router.put("/plans/:id", authenticate, (req, res) => {
  /*
  #swagger.tags = ['ShiftPlanner']
  #swagger.summary = 'Update shift plan'
  #swagger.security = [{ "bearerAuth": [] }]
  */
  shiftPlannerController.updateShiftPlan(req, res);
});

// Pattern plan routes
router.get("/pattern-plans/:id/edit", authenticate, (req, res) => {
  /*  #swagger.tags = ['ShiftPlanner']
  #swagger.summary = 'Get pattern plan details for editing'
  #swagger.security = [{ "bearerAuth": [] }]
  */
  shiftPlannerController.getPatternPlanForEdit(req, res);
});

router.put("/pattern-plans/:id", authenticate, (req, res) => {
  /*
  #swagger.tags = ['ShiftPlanner']
  #swagger.summary = 'Update pattern plan'
  #swagger.security = [{ "bearerAuth": [] }]
  */
  shiftPlannerController.updatePatternPlan(req, res);
});

/**
 * @swagger
 * /shift-planner/plans/{id}:
 *   delete:
 *     tags: [ShiftPlanner]
 *     summary: Delete shift plan
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: string }
 *     responses:
 *       200:
 *         description: Plan deleted successfully
 */
router.delete("/plans/:id", authenticate, (req, res) => {
  shiftPlannerController.deleteShiftPlan(req, res);
});

/**
 * @swagger
 * /shift-planner/groups/{id}:
 *   delete:
 *     tags: [ShiftPlanner]
 *     summary: Delete a group
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema: { type: string }
 *     responses:
 *       200:
 *         description: Group deleted successfully
 */
router.delete("/groups/:id", authenticate, (req, res) => {
  shiftPlannerController.deleteGroup(req, res);
});

module.exports = router;