const productFunction = require("../function/product");
const shopFunction = require("../function/admin");

const addProduct = async (req, res) => {
    try {
        const product = await productFunction.addProduct(req);
        return res.status(200).json({
            success: true,
            msg: "Product Added Successfully!",
            data: product
        })
    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
};

const getProductById = async (req, res) => {
    try {
        const product = await productFunction.getProduct(req);
        return res.status(200).json({
            success: true,
            msg: "Product Details By Id!",
            data: product
        })
    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
};

const updateProduct = async (req, res) => {
    try {
        const product = await productFunction.updateProduct(req);
        return res.status(200).json({
            success: true,
            msg: "Product Details Updated!",
            data: product
        })
    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
};

const getAllProductsByAdminId = async (req, res) => {
    try {
        const products = await productFunction.getAllProductsByAdminId(req);
        return res.status(200).json({
            success: true,
            msg: "All Products By Admin!",
            data: products
        })
    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
};

const getProductsByCategory = async (req, res) => {
    try {
        const products = await productFunction.getAllProductsByCategory(req); 
        return res.status(200).json({
            success: true,
            msg: "All Products By Category!",
            data: products
        })
    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
};

const addVariants = async (req, res) => {
    try {
        const product = await productFunction.addProduct(req);
        if(!product){
            return res.status(200).json({
                success: true,
                msg: "Varian is Not Added!"
            })
        } else {
            const { productId } = req.body;
            const variantId = product._id;
            const variant = await productFunction.addToVariant(productId, variantId);
            return res.status(200).json({
                success: true,
                msg: "Variant is Added Successfully!",
                data: variant
            })
        }
    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
};

const deleteProduct = async (req, res) => {
    try {
        const product = await productFunction.deleteProduct(req);
        return res.status(200).json({
            success: true,
            msg: "Product is Successfully Deleted!"
        })
    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
};

const searchProducts = async (req, res) => {
    try {
        const result = await productFunction.searchProduct(req);
        if(result.length === 0){
            const shops = await shopFunction.getAllShops(req);
            if(shops.length === 0){
                return res.status(200).json({
                    success: true,
                    msg: "No Product or Shop Found!",
                })
            } else {
                return res.status(200).json({
                    success: true,
                    msg: "Searched Shop Results!",
                    data: shops
                })
            }
        } else {
            return res.status(200).json({
                success: true,
                msg: "Searched Product Results!",
                data: result
            })
        }

    } catch (error) {
        console.log("Having Erros: ", error);
        return res.status(200).json({
            success: false,
            msg: "Having Errors",
            error: error.message
        })
    }
}

module.exports = {
    addProduct,
    getProductById,
    updateProduct,
    getAllProductsByAdminId,
    getProductsByCategory,
    addVariants,
    deleteProduct,
    searchProducts
};