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

exports.getCategoriesWithProducts = async (req, res) => {
  try {
    const categories = await Category.find().select("_id name image").sort({
      name: 1,
    });

    const result = await Promise.all(
      categories.map(async (category) => {
        const products = await Product.find({
          category: category._id,
        })
          .populate("category", "name")
          .populate("marble_space", "name")
          .select(
            `
              _id
              product_name
              short_description
              availability
              product_images
              category
              marble_space
            `,
          )
          .sort({
            _id: -1,
          })
          .limit(12);

        return {
          _id: category._id,

          name: category.name,

          image: category.image,

          total_products: products.length,

          products,
        };
      }),
    );

    res.status(200).json({
      success: true,
      total_categories: result.length,
      data: result,
    });
  } catch (error) {
    console.error(error);

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

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

    res.status(200).json({
      success: true,
      total: products.length,
      data: products,
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: error.message,
    });
  }
};

exports.getMarbleSpacesWithProducts = async (req, res) => {
  try {
    const spaces = await MarbleSpace.find();

    const result = await Promise.all(
      spaces.map(async (space) => {
        const products = await Product.find({
          marble_space: space._id,
        })
          .select("product_name product_images")
          .limit(10);

        return {
          _id: space._id,
          name: space.name,
          products,
        };
      }),
    );

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

exports.getAllProducts = async (req, res) => {
  try {
    const products = await Product.find()
      .populate("category", "name")
      .populate("marble_space", "name")
      .select(
        `
        _id
        product_name
        short_description
        availability
        product_images
        category
        marble_space
      `,
      )
      .sort({
        _id: -1,
      });

    res.status(200).json({
      success: true,
      total: products.length,
      data: products,
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: error.message,
    });
  }
};

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

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

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