const couponFunctions = require("../function/coupoun");

const createCoupon = async (req, res) => {
    try {
        const exist = await couponFunctions.findCoupon(req);
        if(!exist){
            const coupon = await couponFunctions.createCoupon(req);
            return res.status(200).json({
                success: true,
                msg: "Coupon is Created!",
                data: coupon
            });
        } else {
            return res.status(200).json({
                success: true,
                msg: "Coupon Already Exist!",
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "having Errors!",
            error: error.message
        })
    }
};

const getAllCouponsByAdminId = async (req, res) => {
    try {
        const coupon = await couponFunctions.getAllCouponsByAdminId(req);
        if( coupon.length === 0 ){
            return res.status(200).json({
                success: true,
                msg: "Can Not Find Any Coupons By AdminId",
            });
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Coupons By AdminId",
                data: coupon
            });
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "having Errors!",
            error: error.message
        })
    }
};

const getCouponById = async (req, res) => {
    try {
        const coupon = await couponFunctions.getCouponById(req);
        return res.status(200).json({
            success: true,
            msg: "Coupon Details!",
            data: coupon
        });
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "having Errors!",
            error: error.message
        })
    }
};

const updateCoupon = async (req, res) => {
    try {
        const coupon = await couponFunctions.updateCoupon(req);
        return res.status(200).json({
            success: true,
            msg: "All Coupons By AdminId",
            data: coupon
        });
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "having Errors!",
            error: error.message
        })
    }
};

const getAllCoupons = async (req, res) => {
    try {
        const coupon = await couponFunctions.getAllCoupOns(req);
        if( coupon.length === 0){
            return res.status(200).json({
                success: true,
                msg: "No Coupon Found!",
            });
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Coupons!",
                data: coupon
            });
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "having Errors!",
            error: error.message
        })
    }
};

const deleteCoupon = async (req, res) => {
    try {
        const coupon = await couponFunctions.deleteCoupOns(req);
        return res.status(200).json({
            success: true,
            msg: "Coupon is Deleted!"
        });
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "having Errors!",
            error: error.message
        })
    }
};

const getCoupon = async (req, res) => {
    try {
        const coupon = await couponFunctions.findCoupon(req);
        return res.status(200).json({
            success: true,
            msg: "Coupon Details!",
            data: coupon
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "having Errors!",
            error: error.message
        })
    }
};

module.exports = {
    createCoupon,
    getAllCouponsByAdminId,
    getCouponById,
    updateCoupon,
    getAllCoupons,
    deleteCoupon,
    getCoupon
};