const express = require("express");
const router = express.Router();
const attendanceController = require("../../controllers/hrandpayrollsController/attendanceManagementController");
const authenticate = require("../../middlewares/authenticate");

/**
 * @swagger
 * tags:
 *   name: Attendance Management
 *   description: Attendance management — daily reset, check-in/out, status updates, reports
 */

router.post("/create", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Create a new attendance management record'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["employee_id", "attendance_date", "org_id"], properties: { employee_id: { type: "string" }, attendance_date: { type: "string", format: "date" }, check_in_time: { type: "string" }, check_out_time: { type: "string" }, attendance_status: { type: "string", enum: ["Present","Absent","Late","Half Day","Cross Shift","Shift Swap","Double Shift","Holiday","Leave"] }, org_id: { type: "integer" } } } } } }
    #swagger.responses[201] = { description: "Attendance management record created" }
  */
  attendanceController.createAttendance(req, res);
});

router.post("/check-in", (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Mark check-in for an employee'
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["employee_id", "attendance_date", "org_id"], properties: { employee_id: { type: "string" }, attendance_date: { type: "string", format: "date" }, check_in_time: { type: "string" }, latitude: { type: "number" }, org_id: { type: "integer" } } } } } }
    #swagger.responses[200] = { description: "Check-in successful" }
  */
  attendanceController.checkIn(req, res);
});

// router.post("/check-out", (req, res) => {
//   /*
//     #swagger.tags = ['Attendance Management']
//     #swagger.summary = 'Mark check-out for an employee'
//     #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["employee_id", "attendance_date", "org_id"], properties: { employee_id: { type: "string" }, attendance_date: { type: "string", format: "date" }, check_out_time: { type: "string" }, org_id: { type: "integer" } } } } } }
//     #swagger.responses[200] = { description: "Checked out successfully" }
//   */
//   attendanceController.checkOut(req, res);
// });

router.put("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Update attendance management record by ID'
    #swagger.parameters['id'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = { content: { "application/json": { schema: { type: "object", properties: { attendance_status: { type: "string" }, check_in_time: { type: "string" }, work_hours: { type: "number" }, late_minutes: { type: "integer" } } } } } }
  */
  attendanceController.updateAttendance(req, res);
});

router.put("/individual/:id/status", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Update attendance management status for a single employee'
    #swagger.parameters['id'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["attendance_status"], properties: { attendance_status: { type: "string", enum: ["Present","Absent","Late","Half Day","Cross Shift","Shift Swap","Double Shift","Holiday","Leave"] }, late_minutes: { type: "integer" }, remarks: { type: "string" } } } } } }
  */
  attendanceController.updateIndividualStatus(req, res);
});

router.put("/individual/:id/checkout", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Checkout a single employee by attendance management record ID'
    #swagger.parameters['id'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = { content: { "application/json": { schema: { type: "object", properties: { check_out_time: { type: "string" } } } } } }
  */
  attendanceController.individualCheckout(req, res);
});

router.post("/bulk/mark", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Bulk mark attendance management status for multiple employees'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["employee_ids", "status", "date", "org_id"], properties: { employee_ids: { type: "array", items: { type: "string" } }, status: { type: "string", enum: ["Present","Absent","Late","Half Day","Leave"] }, date: { type: "string", format: "date" }, org_id: { type: "integer" } } } } } }
  */
  attendanceController.bulkMarkAttendance(req, res);
});

router.post("/bulk/checkout", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Bulk checkout multiple employees'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["employee_ids", "date", "org_id"], properties: { employee_ids: { type: "array", items: { type: "string" } }, date: { type: "string", format: "date" }, checkout_time: { type: "string" }, org_id: { type: "integer" } } } } } }
  */
  attendanceController.bulkCheckout(req, res);
});

router.post("/daily-reset", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Daily reset — creates Present records for all employees'
    #swagger.security = [{ "bearerAuth": [] }]
    #swagger.requestBody = { required: true, content: { "application/json": { schema: { type: "object", required: ["org_id"], properties: { org_id: { type: "integer" } } } } } }
  */
  attendanceController.dailyReset(req, res);
});

router.get("/today", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = "Get today's attendance management grouped by status"
    #swagger.parameters['org_id'] = { in: 'query', required: true, type: 'integer' }
    #swagger.parameters['shift_id'] = { in: 'query', required: false, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getTodayAttendance(req, res);
});

router.get("/all", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get all attendance management records with filters'
    #swagger.parameters['page'] = { in: 'query', type: 'integer', default: 1 }
    #swagger.parameters['limit'] = { in: 'query', type: 'integer', default: 10 }
    #swagger.parameters['employee_id'] = { in: 'query', type: 'string' }
    #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: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getAttendance(req, res);
});

router.get("/summary", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get attendance management summary per employee'
    #swagger.parameters['employee_code'] = { in: 'query', type: 'string' }
    #swagger.parameters['month'] = { in: 'query', type: 'integer' }
    #swagger.parameters['year'] = { in: 'query', type: 'integer' }
    #swagger.parameters['org_id'] = { in: 'query', type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getAttendanceSummary(req, res);
});

router.get("/late-report", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get late attendance management report'
    #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: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getLateAttendanceReport(req, res);
});

router.get("/employees/by-group", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get employees in a group with attendance management status'
    #swagger.parameters['group_id'] = { in: 'query', required: true, type: 'integer' }
    #swagger.parameters['date'] = { in: 'query', type: 'string', format: 'date' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getEmployeesByGroup(req, res);
});

router.get("/employees/by-shift", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get employees in a shift with attendance management status'
    #swagger.parameters['shift_id'] = { in: 'query', required: true, type: 'integer' }
    #swagger.parameters['org_id'] = { in: 'query', required: true, type: 'integer' }
    #swagger.parameters['date'] = { in: 'query', type: 'string', format: 'date' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getEmployeesByShift(req, res);
});

router.get("/shift-plan-schedule", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get shift plan schedule for calendar view'
    #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: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getShiftPlanSchedule(req, res);
});

router.get("/shifts/all", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get all active shifts for an org'
    #swagger.parameters['org_id'] = { in: 'query', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getShifts(req, res);
});

router.get("/transactions", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get attendance management transactions for an employee'
    #swagger.parameters['employee_id'] = { in: 'query', required: true, type: 'string' }
    #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', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getEmployeeTransactions(req, res);
});

router.get("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Get attendance management record by ID'
    #swagger.parameters['id'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.getAttendanceById(req, res);
});

router.delete("/:id", authenticate, (req, res) => {
  /*
    #swagger.tags = ['Attendance Management']
    #swagger.summary = 'Soft delete an attendance management record'
    #swagger.parameters['id'] = { in: 'path', required: true, type: 'integer' }
    #swagger.security = [{ "bearerAuth": [] }]
  */
  attendanceController.deleteAttendance(req, res);
});

module.exports = router;