const couponModel = require("../models/coupoun");

const findCoupon = async (req) => {
    const { couponCode } =  req.query;
    const result = await couponModel.findOne({couponCode: couponCode});
    return result;
};

const createCoupon = async (req) => {
    const image = req.file.filename;
    // console.log("CoupOn Image :", image);
    // return

    const newCoupon = new couponModel(req.body);
    newCoupon.couponImage = image
    const result = await newCoupon.save();
    return result;
};

const updateCoupon = async (req) => {
    const { couponId } = req.body;
    const updatedData = req.body;
    const result = await couponModel.findByIdAndUpdate({_id: couponId},
        { $set: updatedData },
        { new: true }
    );
    return result
};

const getCouponById = async (req) => {
    const { couponId } = req.query;
    const result = await couponModel.findById({_id: couponId});
    return result
};

const getAllCouponsByAdminId = async (req) => {
    const { adminId } = req.query;
    const result = await couponModel.find({couponBy: adminId});
    return result
};

const getAllCoupOns = async (req) => {
    const result = await couponModel.find();
    return result
};

const deleteCoupOns = async (req) => {
    const { couponId } = req.query;
    const result = await couponModel.findByIdAndDelete({_id: couponId});
    return result;
};

module.exports = {
    findCoupon,
    createCoupon,
    updateCoupon,
    getCouponById,
    getAllCouponsByAdminId,
    getAllCoupOns,
    deleteCoupOns
};
