const productModel = require("../models/productModel");

const addProduct = async (req) => {
    const imagePaths = req.files.map(files => req.files[0].filename);

    const newProduct = new productModel({
        ...req.body,
        productImages: imagePaths
    });
    const result = await newProduct.save();
    return result
};

const getProduct = async (req) => {
    const { productId } = req.query;
    const product = await productModel.findById({_id: productId}).populate({
        path: "shopId",
        select: "barName barDetails shopImage"
    }).populate("variants");
    return product
};

const updateProduct = async (req) => {
    const { productId } = req.body;
    const updatedData = req.body;

    if(req.files && req.files.length > 0 ){
        const imagePaths = req.files.map(files => req.files[0].filename);
        console.log("first", imagePaths)
        const result = await productModel.findByIdAndUpdate({_id: productId}, 
            { $set: {...updatedData, productImages: imagePaths}, 
                // $push: {productImages: imagePaths}
                },
            {new: true}
        );
        return result;
    } else {
        const result = await productModel.findByIdAndUpdate({_id: productId}, 
            { $set: {...updatedData}},
            {new: true}
        );
        return result;
    }
};

const getAllProductsByAdminId = async (req) => {
    const { adminId, category } = req.query;
    const filter = {};
    if(adminId){
        filter.adminId = adminId
    }
    if(category){
        filter.category = category
    }
    const result = await productModel.find(filter).populate("variants");
    return result;
};

const getAllProductsByCategory = async (req) =>{
    const { category } = req.query;
    // Set THE Category ID IN PRODUCT SCHEMA 
    const result = await productModel.find({category: category}).populate("variants");
    return result;
};

const addToVariant = async (productId, variantId) => {
    const result = await productModel.findByIdAndUpdate({_id: productId},
        { $push: {variants: variantId}},
        { new: true}
    );
    return result
};

const deleteProduct = async (req) => {
    const { productId } = req.query;
    const result = await productModel.findByIdAndDelete({_id: productId});
    return result
};

const searchProduct = async (req) => {
    const { name, category, Rating, minPrice, maxPrice } = req.query;
    const filter = {}

    if(name){
        filter.name = { $regex: name, $options: 'i' }
    }
    if(category){
        filter.category = category
    }
    if(Rating){
        filter.starRating = Rating
    }
    if (minPrice || maxPrice) {
        filter.price = {};
        if (minPrice) {
            filter.price.$gte = parseFloat(minPrice);
        }
        if (maxPrice) {
            filter.price.$lte = parseFloat(maxPrice);
        }
    }

    const search = await productModel.find(filter).populate("category").populate("variants");
    return search
};

module.exports = { 
    addProduct,
    getProduct,
    updateProduct,
    getAllProductsByAdminId,
    getAllProductsByCategory,
    addToVariant,
    deleteProduct,
    searchProduct
};