const serviceFunctions = require("../functions/service");

const addService = async (req, res) => {
    try {
        const validate = await serviceFunctions.validateService(req);
        if(!validate){
            const service = await  serviceFunctions.addService(req);
            return res.status(200).json({
                success: true,
                msg: "Service Added Successfully!",
                data: service
            })
        } else {
            return res.status(200).json({
                success: false,
                msg: "Service Already Exist!"
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const getAllServices = async (req, res) => {
    try {
        const services = await serviceFunctions.getAllServices(req);
        if(services.length === 0){
            return res.status(200).json({
                success: false,
                msg: "No Services Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "All Services!",
                data: services
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const updateService = async (req, res) => {
    try {
        const service = await serviceFunctions.updateService(req);
        if(!service){
            return res.status(200).json({
                success: false,
                msg: "No Service Found!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Service is Updated Successfully!",
                data: service
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

const deleteService = async (req, res) => {
    try {
        const service = await serviceFunctions.deleteService(req);
        if(!service){
            return res.status(200).json({
                success: false,
                msg: "No Service Found to Delete!"
            })
        } else {
            return res.status(200).json({
                success: true,
                msg: "Service is Deleted!"
            })
        }
    } catch (error) {
        console.log("Having Errors :", error);
        return res.status(403).json({
            success: false,
            msg: "Something Went Wrong!",
            error: error.message
        })        
    }
};

module.exports = {
    addService,
    getAllServices,
    updateService,
    deleteService
};