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

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

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

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

router.post(
  "/assign",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Assignments']
      #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" },
                reason: { type: "string" },
                approved_by: { type: "integer" },
                org_id: { type: "string" },
                created_by: { type: "string" }
              },
              required: ["employee_code", "shift_id", "assignment_date", "org_id"]
            }
          }
        }
      }
    */

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

/* ===========================
   ✅ BULK ASSIGN SHIFT
=========================== */

router.post(
  "/bulk-assign",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Assignments']
      #swagger.summary = 'Bulk assign shift to multiple employees'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    shiftAssignmentsController.bulkAssignShift(req, res);
  }
);

/* ===========================
   ✅ GET ALL SHIFT ASSIGNMENTS
=========================== */

router.get(
  "/all",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Assignments']
      #swagger.summary = 'Get all shift assignments with filters'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['page'] = { in: 'query', type: 'integer' }
      #swagger.parameters['limit'] = { in: 'query', type: 'integer' }
      #swagger.parameters['employee_id'] = { in: 'query', type: 'string' }
      #swagger.parameters['shift_id'] = { in: 'query', type: 'integer' }
      #swagger.parameters['is_active'] = { in: 'query', type: 'boolean' }
      #swagger.parameters['start_date'] = { in: 'query', type: 'string', format: 'date' }
      #swagger.parameters['end_date'] = { in: 'query', type: 'string', format: 'date' }
      #swagger.parameters['org_id'] = { in: 'query', type: 'string' }
    */

    shiftAssignmentsController.getShiftAssignments(req, res);
  }
);

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

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

    shiftAssignmentsController.getShiftAssignmentById(req, res);
  }
);

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

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

    shiftAssignmentsController.updateShiftAssignment(req, res);
  }
);

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

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

    shiftAssignmentsController.deleteShiftAssignment(req, res);
  }
);

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

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

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

/* ===========================
   ✅ GET EMPLOYEE SHIFT HISTORY
=========================== */

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

    shiftAssignmentsController.getEmployeeShiftHistory(req, res);
  }
);

/* ===========================
   ✅ GET ASSIGNMENTS BY SHIFT
=========================== */

router.get(
  "/shift/:shift_id",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Assignments']
      #swagger.summary = 'Get all assignments for a specific shift'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['shift_id'] = { in: 'path', required: true, type: 'integer' }
      #swagger.parameters['is_active'] = { in: 'query', type: 'boolean' }
    */

    shiftAssignmentsController.getAssignmentsByShift(req, res);
  }
);

/* ===========================
   ✅ DEACTIVATE SHIFT ASSIGNMENT
=========================== */

router.put(
  "/deactivate",
  authenticate,
  (req, res) => {
    /*
      #swagger.tags = ['Shift Assignments']
      #swagger.summary = 'Deactivate current shift assignment for an employee'
      #swagger.security = [{ "bearerAuth": [] }]
    */

    shiftAssignmentsController.deactivateShiftAssignment(req, res);
  }
);

module.exports = router;