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

const AuthService =
require("../services/auth.service");

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

class AuthController {

    /**
     * ======================================================
     * Login
     * ======================================================
     */
    async login(req, res) {

        try {

            const result =

                await AuthService.login(

                    req.body,

                    req

                );

            if (!result.success) {

                return res.status(400).json(

                    Response.error(

                        result.message ||

                        "Login failed.",

                        result.errors ||

                        null

                    )

                );

            }

            return res.json(

                Response.success(

                    "Login successful.",

                    {

                        token: result.token,

                        user: result.user

                    }

                )

            );

        }

        catch (error) {

            console.error(error);

            return res.status(500).json(

                Response.error(

                    "Internal Server Error"

                )

            );

        }

    }

    /**
     * ======================================================
     * Current User
     * ======================================================
     */
    async me(req, res) {

        try {

            const result =

                await AuthService.me(

                    req.user.id

                );

            if (!result.success) {

                return res.status(404).json(

                    Response.error(

                        result.message

                    )

                );

            }

            return res.json(

                Response.success(

                    "User fetched successfully.",

                    result.user

                )

            );

        }

        catch (error) {

            console.error(error);

            return res.status(500).json(

                Response.error(

                    "Internal Server Error"

                )

            );

        }

    }

    /**
     * ======================================================
     * Change Password
     * ======================================================
     */
    async changePassword(req, res) {

        try {

            const result =

                await AuthService.changePassword(

                    req.user.id,

                    req.body

                );

            if (!result.success) {

                return res.status(400).json(

                    Response.error(

                        result.message ||

                        "Unable to update password.",

                        result.errors ||

                        null

                    )

                );

            }

            return res.json(

                Response.success(

                    result.message,

                    null

                )

            );

        }

        catch (error) {

            console.error(error);

            return res.status(500).json(

                Response.error(

                    "Internal Server Error"

                )

            );

        }

    }

    /**
     * ======================================================
     * Logout
     * ======================================================
     */
    async logout(req, res) {

        return res.json(

            Response.success(

                "Logout successful.",

                null

            )

        );

    }

}

module.exports =

new AuthController();