 
const Deliverycharges = require('../models/Deliverycharges');
const AppError = require('../utils/appError');
const catchAsync = require('../utils/catchAsync'); 
 

exports.postNew = async function (req, res) {
    let data = req.body;
    console.log("nnnnnn", data)
    if (
        !data
    ) {
        return res.status(422).json({
            message: "All feilds are required",
        });
    }
    try {
        let deliverycharges = await Deliverycharges.findOne();
        if (!deliverycharges) { 
            deliverycharges = new Deliverycharges(data);
            await deliverycharges.save();
        } else { 
            deliverycharges.set(data);
            await deliverycharges.save();
        }
        res.status(200).json({ message: "success", deliverycharges });
    } catch (error) {
        console.log("is match 1st error===>>>", error)
        res.status(404).json({ success: false, message: error.message });
    }
};



exports.postNewRetail = async function (req, res) {
    let data = req.body;
    console.log("nnnnnn", data)
    if (
        !data
    ) {
        return res.status(422).json({
            message: "All feilds are required",
        });
    }
    try {
        let deliverycharges = await Deliverycharges.findOne();
        if (!deliverycharges) { 
            deliverycharges = new Deliverycharges(data);
            await deliverycharges.save();
        } else { 
            deliverycharges.set(data);
            await deliverycharges.save();
        }
        res.status(200).json({ message: "success", deliverycharges });
    } catch (error) {
        console.log("is match 1st error===>>>", error)
        res.status(404).json({ success: false, message: error.message });
    }
};



exports.getAll = async function (req, res) {
    try {
        const deliveryCharges = await Deliverycharges.findOne();
        if (!deliveryCharges) {
            return res.status(404).json({ success: false, message: "No data found" });
        }
        res.status(200).json({ success: true, data: deliveryCharges });
    } catch (error) {
        console.log("Error:", error);
        res.status(500).json({ success: false, message: "Internal Server Error" });
    }
};




exports.getAllRetail = async function (req, res) {
    try {
        const deliveryCharges = await Deliverycharges.findOne();
        console.log("abc=====", deliveryCharges)
        if (!deliveryCharges) {
            return res.status(404).json({ success: false, message: "No data found" });
        }
        res.status(200).json({ success: true, data: deliveryCharges });
    } catch (error) {
        console.log("Error:", error);
        res.status(500).json({ success: false, message: "Internal Server Error" });
    }
};
