const Item = require('../models/Item');  // Assuming you have an Item model

// Load all items for a selected company
exports.loadItemsForCompany = async (req, res) => {
  try {
    const companyId = req.query.companyId;  // Get the company ID from the query parameters
    if (!companyId) {
      return res.status(400).json({ message: 'Company ID is required' });
    }

    const items = await Item.find({ company: companyId });  // Find all items associated with the selected company
    res.status(200).json(items);  // Send the items as a response
  } catch (error) {
    console.error('Error loading items:', error);
    res.status(500).json({ message: 'Server error while loading items' });
  }
};

// Add an item to the cart
exports.addItemToCart = async (req, res) => {
  try {
    const { itemId, quantity } = req.body;  // Get item ID and quantity from the request body
    if (!itemId || !quantity) {
      return res.status(400).json({ message: 'Item ID and quantity are required' });
    }

    // Logic to add the item to the cart goes here
    // You can either save it in the database or session

    // Placeholder response - replace with actual logic
    res.status(200).json({ message: 'Item added to the cart successfully' });
  } catch (error) {
    console.error('Error adding item to cart:', error);
    res.status(500).json({ message: 'Server error while adding item to cart' });
  }
};

// Remove an item from the cart
exports.removeItemFromCart = async (req, res) => {
  try {
    const { itemId } = req.params;  // Get the item ID from the route parameters
    if (!itemId) {
      return res.status(400).json({ message: 'Item ID is required' });
    }

    // Logic to remove the item from the cart goes here
    // Placeholder response - replace with actual logic
    res.status(200).json({ message: 'Item removed from the cart successfully' });
  } catch (error) {
    console.error('Error removing item from cart:', error);
    res.status(500).json({ message: 'Server error while removing item from cart' });
  }
};

// Checkout the cart
exports.checkoutCart = async (req, res) => {
  try {
    // Logic for finalizing the cart, such as processing payment or saving the order
    // Placeholder response - replace with actual logic

    res.status(200).json({ message: 'Cart checked out successfully' });
  } catch (error) {
    console.error('Error during checkout:', error);
    res.status(500).json({ message: 'Server error during checkout' });
  }
};
