const refundModel = require("../models/refund");

const createRefund = async (req) => {
    const newRefund = new refundModel(req.body);
    const result = await newRefund.save(); 
    return result
};

const getRefund = async (req) => {
    const { refundId } = req.query;
    const refund = await refundModel.findById(refundId).populate({
        path: "products",
        select: "name brandName productImages"
    });
    return refund;
};

const getAllRefunds = async (req) => {
    const { userId, shopId, status} = req.query;
    const filter = {};

    if(userId){
        filter.userId = userId 
    };

    if(shopId){
        filter.shopId = shopId
    };

    if(status){
        filter.status = status
    };

    const refunds = await refundModel.find(filter);
    return refunds;
};

const updateRefund = async (req) => {
    const { refundId } = req.body;
    const updatedData = req.body;

    const refund = await refundModel.findByIdAndUpdate(refundId, 
        { $set: updatedData },
        { new: true }
    );
    return refund
};

const deleteRefund = async (req) => {
    const { refundId } = req.body;
    const refund = await refundModel.findByIdAndDelete(refundId);
    return refund
};

module.exports = {
    createRefund,
    getRefund,
    getAllRefunds,
    updateRefund,
    deleteRefund
};