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

const shiftsController = require("../../controllers/postgre/shiftsController");
const authenticate = require("../../middlewares/authenticate");

/**
 * @swagger
 * tags:
 *   name: Shift Management
 *   description: Shift creation, assignment, listing, update and deletion APIs
 */

/* ===========================
   ✅ CREATE SHIFT
=========================== */

router.post(
  "/create",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Create a new shift'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              properties: {
                shift_name: { type: "string" },
                shift_code: { type: "string" },
                start_time: { type: "string" },
                end_time: { type: "string" },
                lunch_start: { type: "string" },
                lunch_end: { type: "string" },
                first_break_start: { type: "string" },
                first_break_end: { type: "string" },
                second_break_start: { type: "string" },
                second_break_end: { type: "string" },
                org_id: { type: "string" },
                created_by: { type: "string" }
              },
              required: ["shift_name", "start_time", "end_time", "org_id"]
            }
          }
        }
      }
    */

    shiftsController.createShift(req, res);
  }
);

/* ===========================
   ✅ UPDATE SHIFT
=========================== */

router.put(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Update shift by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Shift ID',
        required: true,
        type: 'integer'
      }
    */

    shiftsController.updateShift(req, res);
  }
);

/* ===========================
   ✅ GET ALL SHIFTS
=========================== */

router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Get all shifts with filters'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['page'] = {
        in: 'query',
        description: 'Page number',
        type: 'integer'
      }
      #swagger.parameters['limit'] = {
        in: 'query',
        description: 'Records per page',
        type: 'integer'
      }
      #swagger.parameters['org_id'] = {
        in: 'query',
        description: 'Organization ID',
        type: 'string'
      }
      #swagger.parameters['search'] = {
        in: 'query',
        description: 'Search by shift name or code',
        type: 'string'
      }
    */

    shiftsController.getShifts(req, res);
  }
);

/* ===========================
   ✅ GET SHIFT BY ID
=========================== */

router.get(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Get shift by ID'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Shift ID',
        required: true,
        type: 'integer'
      }
    */

    shiftsController.getShiftById(req, res);
  }
);

/* ===========================
   ✅ DELETE SHIFT
=========================== */

router.delete(
  "/:id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Delete shift (soft delete)'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        description: 'Shift ID',
        required: true,
        type: 'integer'
      }
    */

    shiftsController.deleteShift(req, res);
  }
);

/* ===========================
   ✅ ASSIGN SHIFT TO EMPLOYEE
=========================== */

router.post(
  "/assign",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Assign shift to an employee'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              properties: {
                employee_code: { type: "string" },
                shift_id: { type: "integer" },
                assignment_date: { type: "string", format: "date" },
                effective_from: { type: "string", format: "date" },
                effective_to: { type: "string", format: "date" },
                is_permanent: { type: "boolean" },
                org_id: { type: "string" },
                created_by: { type: "string" }
              },
              required: ["employee_code", "shift_id", "assignment_date", "org_id"]
            }
          }
        }
      }
    */

    shiftsController.assignShiftToEmployee(req, res);
  }
);

/* ===========================
   ✅ GET EMPLOYEE CURRENT SHIFT
=========================== */

router.get(
  "/employee/:employee_code/current",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Get employee current shift assignment'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['employee_code'] = {
        in: 'path',
        description: 'Employee code',
        required: true,
        type: 'string'
      }
    */

    shiftsController.getEmployeeCurrentShift(req, res);
  }
);

/* ===========================
   ✅ GET EMPLOYEES BY SHIFT
=========================== */

router.get(
  "/employees/by-shift",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Get all employees assigned to a shift'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['shift_id'] = {
        in: 'query',
        description: 'Shift ID',
        type: 'integer'
      }
      #swagger.parameters['org_id'] = {
        in: 'query',
        description: 'Organization ID',
        type: 'string'
      }
    */

    shiftsController.getEmployeesByShift(req, res);
  }
);

/* ===========================
   ✅ GET SHIFT ASSIGNMENT HISTORY
=========================== */

router.get(
  "/employee/:employee_code/history",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Get shift assignment history for an employee'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['employee_code'] = {
        in: 'path',
        description: 'Employee code',
        required: true,
        type: 'string'
      }
      #swagger.parameters['limit'] = {
        in: 'query',
        description: 'Number of records to fetch',
        type: 'integer'
      }
    */

    shiftsController.getShiftAssignmentHistory(req, res);
  }
);

/* ===========================
   ✅ GET SHIFT FOR ATTENDANCE
=========================== */

router.get(
  "/attendance/shift",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Management']
      #swagger.summary = 'Get shift details for attendance marking'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['employee_code'] = {
        in: 'query',
        description: 'Employee code',
        required: true,
        type: 'string'
      }
      #swagger.parameters['attendance_date'] = {
        in: 'query',
        description: 'Attendance date',
        required: true,
        type: 'string',
        format: 'date'
      }
    */

    shiftsController.getShiftForAttendance(req, res);
  }
);
/* ===========================
   ✅ DEACTIVATE SHIFT ASSIGNMENT
=========================== */
router.put(
  "/assignments/deactivate",
  authenticate,
  (req, res) => {
    shiftsController.deactivateShiftAssignment(req, res);
  }
);


// In shiftsRoutes.js
router.post("/bulk-assign", authenticate, (req, res) => {
  shiftsController.bulkAssignShift(req, res);
});

/* ===========================
   ✅ UPDATE EMPLOYEE SHIFT WITH ATTENDANCE PROPAGATION
=========================== */

router.put("/employee/shift-with-attendance", authenticate, (req, res) => {
  shiftsController.updateEmployeeShiftWithAttendance(req, res);
});

/* ===========================
   ✅ BULK UPDATE EMPLOYEE SHIFT WITH ATTENDANCE PROPAGATION
=========================== */

router.put("/employees/shift-with-attendance/bulk", authenticate, (req, res) => {
  shiftsController.bulkUpdateEmployeeShiftWithAttendance(req, res);
});

module.exports = router;