

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

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

/**
 * @swagger
 * tags:
 *   name: Attendance Buffer
 *   description: Monthly summaries, timesheets, department reports, overtime and late analytics
 */

/* ===========================
   ✅ UPDATE BUFFER TABLE
=========================== */
router.post("/update", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Manually sync buffer table from attendance table'
    #swagger.description = 'Use this when buffer is out of sync. Upserts records including boolean day-type flags.'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = {
      required: false,
      content: {
        "application/json": {
          schema: {
            type: "object",
            properties: {
              date:        { type: "string", format: "date", description: "Filter by date (optional)" },
              employee_id: { type: "string", description: "Employee code (optional)" },
              org_id:      { type: "integer", description: "Organization ID (optional, syncs all if omitted)" }
            }
          }
        }
      }
    }
    #swagger.responses[200] = { description: "Buffer updated, returns rows affected" }
  */
  bufferController.updateBufferTable(req, res);
});

/* ===========================
   ✅ RECALCULATE ALL BUFFER DATA
=========================== */
router.post("/recalculate", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Full recalculate — wipes and rebuilds buffer for an org'
    #swagger.description = 'Soft-deletes all existing buffer records for the org, then re-inserts from attendance table.'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = {
      required: true,
      content: {
        "application/json": {
          schema: {
            type: "object",
            required: ["org_id"],
            properties: {
              org_id: { type: "integer" }
            }
          }
        }
      }
    }
    #swagger.responses[200] = { description: "Recalculation complete, returns records_processed count" }
  */
  bufferController.recalculateAllBuffer(req, res);
});

/* ===========================
   ✅ GET MONTHLY SUMMARY
=========================== */
router.get("/monthly-summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Monthly attendance summary per employee'
    #swagger.description = 'Returns present_days, absent_days, late_days, holiday_days, total_hours, total_overtime, avg_daily_hours etc.'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['month']       = { in: 'query', required: true,  type: 'integer', description: '1–12' }
    #swagger.parameters['year']        = { in: 'query', required: true,  type: 'integer', description: 'YYYY' }
    #swagger.parameters['org_id']      = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['employee_id'] = { in: 'query', required: false, type: 'string',  description: 'Employee code to filter single employee' }
    #swagger.responses[200] = {
      description: "Monthly summary list",
      schema: {
        success: true,
        data: [],
        summary: { month: "1", year: "2024", month_year: "2024-01", total_employees: 0 }
      }
    }
  */
  bufferController.getMonthlySummary(req, res);
});

/* ===========================
   ✅ GET DAILY BUFFER DATA
=========================== */
router.get("/daily", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Get buffer records for a specific date'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['date']   = { in: 'query', required: true, type: 'string', format: 'date' }
    #swagger.parameters['org_id'] = { in: 'query', required: true, type: 'integer' }
    #swagger.responses[200] = { description: "Buffer rows for that date with employee department/designation" }
  */
  bufferController.getDailyBufferData(req, res);
});

/* ===========================
   ✅ GET EMPLOYEE TIMESHEET
=========================== */
router.get("/employee-timesheet", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Full monthly timesheet for a single employee'
    #swagger.description = 'Returns day-by-day attendance with summary totals (total hours, overtime, late minutes, day counts).'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['employee_code'] = { in: 'query', required: true,  type: 'string' }
    #swagger.parameters['month']         = { in: 'query', required: true,  type: 'integer', description: '1–12' }
    #swagger.parameters['year']          = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['org_id']        = { in: 'query', required: true,  type: 'integer' }
    #swagger.responses[200] = {
      description: "Timesheet rows + summary",
      schema: {
        success: true,
        data: [],
        summary: {
          total_days: 0, present_days: 0, absent_days: 0, late_days: 0,
          holiday_days: 0, leave_days: 0, half_days: 0, double_shift_days: 0,
          total_hours: 0, total_overtime: 0, total_late_minutes: 0
        }
      }
    }
  */
  bufferController.getEmployeeTimesheet(req, res);
});

/* ===========================
   ✅ GET DEPARTMENT MONTHLY SUMMARY
=========================== */
router.get("/department-summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Monthly attendance summary grouped by department'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['month']      = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['year']       = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['org_id']     = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['department'] = { in: 'query', required: false, type: 'string', description: 'Filter to a single department' }
    #swagger.responses[200] = { description: "Per-department totals" }
  */
  bufferController.getDepartmentMonthlySummary(req, res);
});

/* ===========================
   ✅ GET OVERTIME REPORT
=========================== */
router.get("/overtime-report", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Overtime report for a month — employees ranked by total overtime'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['month']        = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['year']         = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['org_id']       = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['min_overtime'] = { in: 'query', required: false, type: 'number', default: 0, description: 'Minimum total overtime hours to include' }
    #swagger.responses[200] = {
      description: "Overtime list with summary totals",
      schema: {
        success: true,
        data: [],
        summary: { total_overtime_hours: 0, total_employees: 0 }
      }
    }
  */
  bufferController.getOvertimeReport(req, res);
});

/* ===========================
   ✅ GET LATE REPORT
=========================== */
router.get("/late-report", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Late report for a month — employees ranked by total late minutes'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['month']            = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['year']             = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['org_id']           = { in: 'query', required: true,  type: 'integer' }
    #swagger.parameters['min_late_minutes'] = { in: 'query', required: false, type: 'integer', default: 0 }
    #swagger.responses[200] = {
      description: "Late report with summary",
      schema: {
        success: true,
        data: [],
        summary: { total_late_minutes: 0, total_employees: 0 }
      }
    }
  */
  bufferController.getLateReport(req, res);
});

/* ===========================
   ✅ GET EMPLOYEE COMPARISON (multi-month)
=========================== */
router.get("/employee-comparison", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Buffer']
    #swagger.summary = 'Compare employee attendance across a range of months'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.parameters['employee_code'] = { in: 'query', required: true, type: 'string' }
    #swagger.parameters['start_month']   = { in: 'query', required: true, type: 'integer', description: '1–12' }
    #swagger.parameters['start_year']    = { in: 'query', required: true, type: 'integer' }
    #swagger.parameters['end_month']     = { in: 'query', required: true, type: 'integer', description: '1–12' }
    #swagger.parameters['end_year']      = { in: 'query', required: true, type: 'integer' }
    #swagger.parameters['org_id']        = { in: 'query', required: true, type: 'integer' }
    #swagger.responses[200] = { description: "Monthly comparison rows ordered by month_year" }
  */
  bufferController.getEmployeeComparison(req, res);
});

module.exports = router;