const superAdminFunction = require("../function/superAdmin");
const businessCategoryFunction = require("../function/businessCategory");
const validation = require("../function/validate");
const otpFunction = require("../function/OTP");
const emailFunction = require("../function/sendEmail"); 

// Admin Auth APIs
const signup = async (req, res) => {
    try {
        const validate = await superAdminFunction.validateSuperAdmin(req);
        // console.log("validate Controller :", validate);
        // return
        if(validate){
            return res.status(200).json({
                success: false,
                msg: "Email Already Exist!"
            })
        } else {
            const superAdmin = await superAdminFunction.createSuperAdmin(req);
            return res.status(200).json({
                success: true,
                msg: "SuperAdmin Signed-up!",
                data: {
                    _id: superAdmin._id,
                    email: superAdmin.email
                }
            });
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        });        
    }
};

const login  = async (req, res) => {
    try {
        const validate = await superAdminFunction.validateSuperAdmin(req);
        if(!validate){
            return res.status(200).json({
                success: false,
                msg: "No Account Found!"    
            })
        } else {
            const superAdmin = await superAdminFunction.getSuperAdmin(req);
            const hash = superAdmin.password;
            const { password } = req.body;
            const verify = await validation.verifyPassword(password, hash);
            if(!verify){
                return res.status(200).json({
                    success: false,
                    msg: "Incorret Password!"
                })
            } else {
                return res.status(200).json({
                    success: true,
                    msg: "Super Admin Logged-In!",
                    data: {
                        _id: superAdmin._id,
                        email: superAdmin.email
                    }
                })
            }
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        });  
    }
}

const getSuperAdmin = async (req, res) => {
    try {
        const superAdmin = await superAdminFunction.getSuperAdminProfile(req);
        if(!superAdmin){
            return res.status(200).json({
                success: false,
                msg: "Super Admin Not Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Super Admin Details!",
                data: superAdmin,
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        });        
    }
};

const updateSuperAdmin = async (req, res) => {
    try {
        const superAdmin = await superAdminFunction.updateSuperAdmin(req);
        return res.status(200).json({
            success: true,
            msg: "Admin Details Updated!",
            data: superAdmin
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        });
    }
};

const forgetPassword = async (req, res) => {
    try {
        console.log("first :", req.body);
        // return
        const superAdmin = await superAdminFunction.getSuperAdmin(req);
        if (!superAdmin){
            return res.status(200).json({
                success: false,
                msg: "Invalid Email No Account Found!"
            })
        } else {
            const createOTP = await otpFunction.generateOTP(req);
            const userData = {
                email: req.body.email,
                otp: createOTP.OTP
            };
            const sendEmail = await emailFunction.sendOTP(userData);
            return res.status(200).json({
                success: true,
                msg: "OTP Sent!",
                data: {
                    email: userData.email
                }
            });            
        }
    } catch (error) {
        console.log("Having Errors: ", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })        
    }
};

const verifyOTPForPassword = async (req, res) => {
    try {
        const verify = await otpFunction.verfiyOTP(req);
        if(!verify){
            return res.status(200).json({
                success: false,
                msg: "Invalid OTP!"
            })
        } else {
            const superAdmin = await superAdminFunction.getSuperAdmin(req);
            return res.status(200).json({
                success: true,
                msg: "OTP is Successfully Verified!",
                data: {
                    _id: superAdmin._id
                }
            })
        }
    } catch (error) {
        console.log("Having Errors: ", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })        
    }
};

const resetPassword = async (req, res) => {
    try {
        const superAdmin = await superAdminFunction.resetPassword(req);
        if(!superAdmin){
            return res.status(200).json({
                success: false,
                msg: "No SuperAdmin Found to Reset Password!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Password Successfully Changed!"
            })
        }
    } catch (error) {
        console.log("Having Errors: ", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
}

// Feature APIs
const createFeature = async (req, res) => {
    try {
        const feature = await superAdminFunction.createFeature(req);
        return res.status(200).json({
            success: true,
            msg: "Feature is Successfully Created!",
            data: feature
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        });        
    }
};

const getAllFeatures = async (req, res) => {
    try {
        const feature = await superAdminFunction.getAllFeatures(req);
        if(feature.length === 0){
            return res.status(200).json({
                success: false,
                msg: "No Feature Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Features!",
                data: feature
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })        
    }
};

const getFeatureById = async (req, res) => {
    try {
        const feature = await superAdminFunction.getFeatureById(req);
        if(!feature){
            return res.status(200).json({
                success: false,
                msg: "Feature Not Found By Id!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Feature Details By Id!",
                data: feature
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })        
    }
};

const updateFeature = async (req, res) => {
    try {
        const feature = await superAdminFunction.updateFeature(req);
        if(!feature){
            return res.status(200).json({
                success: false,
                msg: "No Feature Found to Update!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Feature Details Updated!",
                data: feature
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })        
    }
};

const deleteFeature = async (req, res) => {
    try {
        const feature = await superAdminFunction.deleteFeature(req);
        if(!feature){
            return res.status(200).json({
                success: false,
                msg: "No Feature Found to Delete!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Feature Is Deleted!"
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })        
    }
};

const addBusinessCategory = async (req, res) => {
    try {
        const exist = await businessCategoryFunction.categoryExist(req);
        if(!exist){
            const businessCategory = await businessCategoryFunction.addBusinessCategory(req);
            return res.status(200).json({
                success: true,
                msg: "Business Category Added!",
                data: businessCategory
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Business Category Already Exists!",
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

const getAllCategories = async (req, res) => {
    try {
        const categories = await businessCategoryFunction.getAllCategories(req);
        return res.status(200).json({
            success: true,
            msg: "All Business Categories!",
            data: categories
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }   
};

const deleteCategory = async (req, res) => {
    try {
        const category = await businessCategoryFunction.deleteCategory(req);
        return res.status(200).json({
            success: true,
            msg: "Business Category Deleted!",

        }) 
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }    
};

const statsForSuperAdmin = async (req, res) => {
    try {
        const stats = await superAdminFunction.superAdminStats(req);
        return res.status(200).json({
            success: true,
            msg: "OverAll Statistics!",
            data: stats
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })
    }
};

module.exports = {
    signup,
    login,
    getSuperAdmin,
    updateSuperAdmin,
    forgetPassword,
    verifyOTPForPassword,
    resetPassword,
    createFeature,
    getAllFeatures,
    getFeatureById,
    updateFeature,
    deleteFeature,
    addBusinessCategory,
    getAllCategories,
    deleteCategory,
    statsForSuperAdmin
};