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

exports.createProduct = async (req, res) => {
  try {
    const {
      category,
      marble_space,

      product_name,
      product_code,
      short_description,
      origin,
      finish,
      thickness,
      availability,

      long_description,
      specification,
      application,

      meta_title,
      meta_description,
    } = req.body;

    const productImages = req.files["product_images"]
      ? req.files["product_images"].map((img) => img.filename)
      : [];

    const galleryImages = req.files["gallery_images"]
      ? req.files["gallery_images"].map((img) => img.filename)
      : [];

    const catalogPdf = req.files["catalog_pdf"]
      ? req.files["catalog_pdf"][0].filename
      : "";

    if (productImages.length === 0) {
      return res.status(400).json({
        message: "At least one product image required",
      });
    }

    const product = await Product.create({
      category,
      marble_space,

      product_name,
      product_code,
      short_description,
      origin,
      finish,
      thickness,
      availability,

      long_description,
      specification,
      application,

      catalog_pdf: catalogPdf,

      product_images: productImages,

      gallery_images: galleryImages,

      meta_title,
      meta_description,
    });

    res.status(201).json({
      success: true,
      product,
    });
  } catch (error) {
    res.status(500).json({
      message: error.message,
    });
  }
};

exports.updateProduct = async (req, res) => {
  try {
    const product = await Product.findById(req.params.id);

    if (!product) {
      return res.status(404).json({
        message: "Product not found",
      });
    }

    Object.assign(product, req.body);

    if (req.files?.product_images) {
      product.product_images = req.files["product_images"].map(
        (img) => img.filename,
      );
    }

    if (req.files?.gallery_images) {
      product.gallery_images = req.files["gallery_images"].map(
        (img) => img.filename,
      );
    }

    if (req.files?.catalog_pdf) {
      product.catalog_pdf = req.files["catalog_pdf"][0].filename;
    }

    await product.save();

    res.json({
      success: true,
    });
  } catch (error) {
    res.status(500).json({
      message: error.message,
    });
  }
};

exports.getProducts = async (req, res) => {
  try {
    const products = await Product.find()
      .populate("category")
      .populate("marble_space")
      .sort({ _id: -1 });

    res.json(products);
  } catch (error) {
    res.status(500).json({
      message: error.message,
    });
  }
};

exports.getProduct = async (req, res) => {
  try {
    const product = await Product.findById(req.params.id)
      .populate("category")
      .populate("marble_space");

    res.json(product);
  } catch (error) {
    res.status(500).json({
      message: error.message,
    });
  }
};

exports.deleteProduct = async (req, res) => {
  try {
    await Product.findByIdAndDelete(req.params.id);

    res.json({
      success: true,
    });
  } catch (error) {
    res.status(500).json({
      message: error.message,
    });
  }
};
