const Product = require("../model/Product")

exports.createProduct = async (req, res) => {
	try {
		const { name, description, price } = req.body

		const newProduct = new Product({ name, description, price })
		await newProduct.save()
		res.status(201).json(newProduct)
	} catch (error) {
		res.status(500).json({ error: error.message })
	}
}

// Controller function for retrieving all products (accessible to both admin and normal user)
exports.getAllProducts = async (req, res) => {
	try {
		const products = await Product.find()

		res.status(200).json(products)
	} catch (error) {
		console.error(error) 
		res.status(500).json({ message: "Internal server error" })
	}
}

// Controller function for retrieving all active products (accessible to both admin and normal user)
exports.getActiveProducts = async (req, res) => {
	try {
		const activeProducts = await Product.find({ isActive: true })

		res.status(200).json(activeProducts)
	} catch (error) {
		console.error(error)
		res.status(500).json({ message: "Internal server error" })
	}
}

// Controller to retrieve a single product by id
exports.getProductById = async (req, res) => {
  const productId = req.params.id;

  try {
    const product = await Product.findById(productId);

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

    res.status(200).json(product);
  } catch (error) {
    res.status(500).json({ message: 'Internal server error' });
  }
};


// Controller function to update product information
exports.updateProduct = async (req, res) => {
  const productId = req.params.id;
  const { name, description, price, isActive } = req.body;

  try {
    // Check if the product exists
    const existingProduct = await Product.findById(productId);
    if (!existingProduct) {
      return res.status(404).json({ message: 'Product not found' });
    }

    // Update product information
    existingProduct.name = name;
    existingProduct.description = description;
    existingProduct.price = price;
    existingProduct.isActive = isActive;

    // Save the updated product
    const updatedProduct = await existingProduct.save();

    res.status(200).json(updatedProduct);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Internal Server Error' });
  }
};

exports.activateProduct = async (req, res) => {
  const { productId } = req.params;

  try {
    // Find the product by ID
    const product = await Product.findById(productId);

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

    // Check if the product is already active
    if (product.isActive) {
      return res.status(400).json({ message: 'Product is already active' });
    }

    // Activate the product
    product.isActive = true;

    // Save the updated product
    await product.save();

    res.json({ message: 'Product activated successfully', product });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Internal Server Error' });
  }
};

exports.archiveProduct = async (req, res) => {
  const { productId } = req.params;

  try {
    // Find the product by ID
    const product = await Product.findById(productId);

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

    // Check if the product is already active
    if (!product.isActive) {
      return res.status(400).json({ message: 'Product is already in Archive' });
    }

    // Activate the product
    product.isActive = false;

    // Save the updated product
    await product.save();

    res.json({ message: 'Product successfuly archived', product });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Internal Server Error' });
  }
};
