/**
 * ==========================================================
 * Smart Attendance System
 * Attendance Controller
 * ==========================================================
 */

const AttendanceService = require("./service");
const Response = require("../../utils/response");

class AttendanceController {

    /**
     * ======================================================
     * Mark Attendance
     * POST /api/attendance/mark
     * ======================================================
     */
    static async markAttendance(req, res) {

        try {

            const {

                face_student_id,

                nfc_uid,

                face_image = null

            } = req.body;

            if (!face_student_id) {

                return res.status(400).json(

                    Response.error(

                        "face_student_id is required."

                    )

                );

            }

            if (!nfc_uid) {

                return res.status(400).json(

                    Response.error(

                        "nfc_uid is required."

                    )

                );

            }

            const result = await AttendanceService.markAttendance({

                face_student_id,

                nfc_uid,

                face_image,

                device_id: req.device?.device_id || null,

                ip_address: req.ip

            });

            if (!result.success) {

                return res.status(400).json(

                    Response.error(

                        result.message

                    )

                );

            }

            return res.status(201).json(

                Response.success(

                    result.message,

                    result.data

                )

            );

        }

        catch (error) {

            console.error("Attendance Error:", error);

            return res.status(500).json({

                success: false,

                message: error.message,

                stack:

                    process.env.NODE_ENV === "development"

                        ? error.stack

                        : undefined

            });

        }

    }

    /**
     * ======================================================
     * Checkout
     * POST /api/attendance/checkout
     * ======================================================
     */
    static async checkout(req, res) {

        try {

            const { student_id } = req.body;

            if (!student_id) {

                return res.status(400).json(

                    Response.error(

                        "student_id is required."

                    )

                );

            }

            const result =

                await AttendanceService.checkout(

                    student_id

                );

            if (!result.success) {

                return res.status(400).json(

                    Response.error(

                        result.message

                    )

                );

            }

            return res.json(

                Response.success(

                    result.message

                )

            );

        }

        catch (error) {

            console.error("Checkout Error:", error);

            return res.status(500).json({

                success: false,

                message: error.message,

                stack:

                    process.env.NODE_ENV === "development"

                        ? error.stack

                        : undefined

            });

        }

    }

    /**
     * ======================================================
     * Today's Attendance
     * GET /api/attendance/today
     * ======================================================
     */
    static async todayAttendance(req, res) {

        try {

            const rows =

                await AttendanceService.todayAttendance();

            return res.json(

                Response.success(

                    "Today's attendance fetched successfully.",

                    rows,

                    {

                        total: rows.length

                    }

                )

            );

        }

        catch (error) {

            console.error("Today Attendance Error:", error);

            return res.status(500).json({

                success: false,

                message: error.message,

                stack:

                    process.env.NODE_ENV === "development"

                        ? error.stack

                        : undefined

            });

        }

    }

    /**
     * ======================================================
     * Student Attendance History
     * GET /api/attendance/student/:id
     * ======================================================
     */
    static async studentHistory(req, res) {

        try {

            const rows =

                await AttendanceService.studentHistory(

                    req.params.id

                );

            return res.json(

                Response.success(

                    "Attendance history fetched successfully.",

                    rows,

                    {

                        total: rows.length

                    }

                )

            );

        }

        catch (error) {

            console.error("Student History Error:", error);

            return res.status(500).json({

                success: false,

                message: error.message,

                stack:

                    process.env.NODE_ENV === "development"

                        ? error.stack

                        : undefined

            });

        }

    }

    /**
     * ======================================================
     * Monthly Report
     * GET /api/attendance/monthly
     * ======================================================
     */
    static async monthlyReport(req, res) {

        try {

            const {

                year,

                month

            } = req.query;

            if (!year || !month) {

                return res.status(400).json(

                    Response.error(

                        "year and month are required."

                    )

                );

            }

            const rows =

                await AttendanceService.monthlyReport(

                    year,

                    month

                );

            return res.json(

                Response.success(

                    "Monthly attendance report fetched successfully.",

                    rows,

                    {

                        total: rows.length

                    }

                )

            );

        }

        catch (error) {

            console.error("Monthly Report Error:", error);

            return res.status(500).json({

                success: false,

                message: error.message,

                stack:

                    process.env.NODE_ENV === "development"

                        ? error.stack

                        : undefined

            });

        }

    }

    /**
     * ======================================================
     * Dashboard Statistics
     * GET /api/attendance/statistics
     * ======================================================
     */
    static async statistics(req, res) {

        try {

            const data =

                await AttendanceService.statistics();

            return res.json(

                Response.success(

                    "Attendance statistics fetched successfully.",

                    data

                )

            );

        }

        catch (error) {

            console.error("Statistics Error:", error);

            return res.status(500).json({

                success: false,

                message: error.message,

                stack:

                    process.env.NODE_ENV === "development"

                        ? error.stack

                        : undefined

            });

        }

    }

}

module.exports = AttendanceController;