const refundFunctions = require("../function/refund");

const createRefundReq = async (req, res) => {
    try {
        const refund = await refundFunctions.createRefund(req);
        return res.status(200).json({
            success: true,
            msg: "Refund Request is Created!",
            data: refund
        })
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })  
    }
};

const getRefundReq = async (req, res) => {
    try {
        const refund = await refundFunctions.getRefund(req);
        if(!refund){
            return res.status(200).json({
                success: false,
                msg: "No Refund Request Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Refund Request Details by Id!",
                data: refund
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })  
    }    
};

const getAllRefundReqs = async (req, res) => {
    try {
        const refunds = await refundFunctions.getAllRefunds(req);
        if(refunds.length === 0){
            return res.status(200).json({
                success: false,
                msg: "No Refund Requests Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Refund Requests Found!",
                data: refunds
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })  
    }    
};

const updateRefundReq = async (req, res) => {
    try {
        const refund = await refundFunctions.updateRefund(req);
        if(!refund){
            return res.status(200).json({
                success: false,
                msg: "No Refund Request Found to Update!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Refund Request is Updated!",
                data: refund
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })  
    }    
};

const deleteRefundReq = async (req, res) => {
    try {
        const refund = await refundFunctions.deleteRefund(req);
        if(!refund){
            return res.status(200).json({
                success: false,
                msg: "No Refund Request Found to Delete!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Refund Request is Deleted!"
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(400).json({
            success: false,
            msg: "Having Errors!",
            error: error.message
        })  
    }
};

module.exports = {
    createRefundReq,
    getRefundReq,
    getAllRefundReqs,
    updateRefundReq,
    deleteRefundReq
};