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

/**
 * @swagger
 * tags:
 *   name: Attendance
 *   description: Attendance management APIs
 */

/**
 * ======================================================
 * GET DAILY ATTENDANCE (MAIN SCREEN)
 * ======================================================
 * Admin / Manager can view attendance for a date
 */
router.get(
  "/",
  authenticate,
  authorize(["admin", "superadmin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Attendance']
      #swagger.summary = 'Get daily attendance by date'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['date'] = {
        in: 'query',
        required: true,
        type: 'string',
        example: '2026-01-06'
      }
    */
    attendanceController.getDailyAttendance(req, res);
  }
);

/**
 * ======================================================
 * MARK / UPDATE ATTENDANCE (MANUAL ENTRY)
 * ======================================================
 * Admin / Manager marks attendance for a user & date
 */
router.post(
  "/mark",
  authenticate,
  authorize(["admin", "superadmin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Attendance']
      #swagger.summary = 'Mark or update attendance'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              required: ["userId", "date"],
              properties: {
                userId: { type: "string" },
                date: { type: "string", example: "2026-01-06" },
                checkIn: { type: "string", example: "09:00 AM" },
                checkOut: { type: "string", example: "06:00 PM" }
              }
            }
          }
        }
      }
    */
    attendanceController.markAttendance(req, res);
  }
);

/**
 * ======================================================
 * RESET ATTENDANCE (MAKE ABSENT)
 * ======================================================
 * Admin / Manager resets attendance back to Absent
 */
router.put(
  "/reset",
  authenticate,
  authorize(["admin", "superadmin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Attendance']
      #swagger.summary = 'Reset attendance to Absent'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              required: ["userId", "date"],
              properties: {
                userId: { type: "string" },
                date: { type: "string", example: "2026-01-06" }
              }
            }
          }
        }
      }
    */
    attendanceController.resetAttendance(req, res);
  }
);

/**
 * ======================================================
 * MONTHLY ATTENDANCE SUMMARY (PER USER)
 * ======================================================
 * Used for payroll & reports
 */
router.get(
  "/monthly",
  authenticate,
  authorize(["admin", "superadmin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Attendance']
      #swagger.summary = 'Get monthly attendance summary for a user'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['userId'] = {
        in: 'query',
        required: true,
        type: 'string'
      }
      #swagger.parameters['month'] = {
        in: 'query',
        required: true,
        type: 'string',
        example: '2026-01'
      }
    */
    attendanceController.getMonthlyAttendance(req, res);
  }
);


router.post("/bulk", authenticate, authorize(["admin", "superadmin", "manager"]), (req, res) => {
  // #swagger.tags = ['Attendance']
  // #swagger.summary = 'Bulk upsert attendance'
  // #swagger.description = 'Create or update attendance records in bulk'
  // #swagger.security = [{ "bearerAuth": [] }]
  attendanceController.bulkUpsertAttendance(req, res)
}

);

router.put(
  "/:id",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    /*
      #swagger.tags = ['Attendance']
      #swagger.summary = 'Update attendance details'
      #swagger.security = [{ "bearerAuth": [] }]
      #swagger.parameters['id'] = {
        in: 'path',
        required: true,
        description: 'Attendance ID'
      }
      #swagger.requestBody = {
        required: true,
        content: {
          "application/json": {
            schema: {
              type: "object",
              properties: {
                checkIn: { type: "string", example: "09:30" },
                checkOut: { type: "string", example: "18:30" },
                statusOverride: {
                  type: "string",
                  enum: ["Present", "Absent", "Half-Day", "Late"],
                  example: "Present"
                }
              }
            }
          }
        }
      }
    */
    attendanceController.updateAttendance(req, res);
  }
);

router.get(
  "/stats",
  authenticate,
  authorize(["admin", "manager"]),
  (req, res) => {
    // #swagger.tags = ['Attendance']
    // #swagger.summary = 'Get attendance statistics'
    // #swagger.description = 'Fetch attendance summary and statistics for employees'
    // #swagger.security = [{ "bearerAuth": [] }]
    return attendanceController.getAttendanceStats(req, res);
  }
);



module.exports = router;
