const adminFunction = require("../functions/admin");
const emailFunction = require("../functions/email");
const validation = require("../functions/validation");
const otpFunction = require("../functions/otp");

const signup = async (req, res) => {
    try {
        const validate = await adminFunction.validateAdmin(req);
        if(validate){
            return res.status(200).json({
                success: false,
                msg: "Email Already Exist!"
            })
        } else {
            const admin = await adminFunction.signup(req);
            return res.status(200).json({
                success: true,
                msg: "Admin Signed-up Successfully!",
                data: admin
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const login = async (req, res) => {
    try {
        const validate = await adminFunction.validateAdmin(req);
        if(!validate){
            return res.status(200).json({
                success: false,
                msg: "No Email Found!"
            })
        } else {
            const { password } = req.body;
            const hash = validate.password;
            const verify = await validation.verifyPassword(password, hash);
            if(!verify){
                return res.status(200).json({
                    success: true,
                    msg: "Incorrect Password!"
                })
            } else {
                return res.status(200).json({
                    success: true,
                    msg: "Admin Logged-In Successfully!",
                    data: validate
                })
            }
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const getAdmin = async (req, res) => {
    try {
        const admin = await adminFunction.validateAdmin(req)
        if(!admin){
            return res.status(200).json({
                success: false,
                msg: "Admin Not Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Admin Details By Id!",
                data: admin
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const updateAdmin = async (req, res) => {
    try {
        const admin = await adminFunction.updateAdmin(req);
        if(!admin){
            return res.status(200).json({
                success: false,
                msg: "Admin Not Found to Update!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Admin Updated Successfully!",
                data: admin
            })
        }        
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const deleteAdmin = async (req, res) => {
    try {
        const admin = await adminFunction.deleteAdmin(req);
        if(!admin){
            return res.status(200).json({
                success: false,
                msg: "Admin Not Found to Delete!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Admin Deleted Successfully!"
            })
        }  
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const forgetPassword = async (req, res) => {
    try {
        const admin = await adminFunction.validateAdmin(req);
        if(!admin){
            return res.status(200).json({
                success: false,
                msg: "No Account Found!"
            })
        } else {
            const createOTP = await otpFunction.generateOTP(req);
            const userData = {
                _id: admin._id,
                email: admin.email,
                OTP: createOTP.OTP
            };

            const sendEmail = await emailFunction.sendOTP(userData);
            return res.status(200).json({
                success: true,
                msg: "OTP Sent Successfully!",
                data: userData
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const resetPassword = async (req, res) => {
    try {
        const admin = await adminFunction.updateAdmin(req);
        return res.status(200).json({
            success: true,
            msg: "Password is Changed Successfully!",
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
}

const changePassword = async (req, res) => {
    try {
        const admin = await adminFunction.getAdmin(req);
        const hash = admin.password;
        const password = req.body.oldPassword;
        const verify = await validation.verifyPassword(password, hash);
        if(!verify){
            return res.status(200).json({
                success: false,
                msg: "Incorrect Password!"
            })
        } else {
            const updatePassword = await adminFunction.updateAdmin(req);
            return res.status(200).json({
                success: true,
                msg: "Password Changed Successfully!"
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

module.exports = {
    signup,
    login,
    getAdmin,
    updateAdmin,
    deleteAdmin,
    forgetPassword,
    resetPassword,
    changePassword
};