const express = require('express');
const router = express.Router();
const cartController = require('../controllers/cartController');  // Ensure this path is correct

const Item = require('../models/Item');  // Assuming you have an Item model
// Load items for the selected company
//router.get('/items', cartController.loadItemsForCompany);

// Add an item to the cart
router.post('/add', cartController.addItemToCart);  // Ensure this is defined in cartController

// Remove an item from the cart
router.delete('/remove/:itemId', cartController.removeItemFromCart);

// Checkout the cart
router.post('/checkout', cartController.checkoutCart);

// Route to get all items for a specific company
router.get('/items', async (req, res) => {
    try {
      const companyId = req.query.companyId;  // Get the company ID from the query parameters
      const items = await Item.find({ company: companyId });  // Find all items associated with the selected company
      res.json(items);  // Return the items as a JSON response
    } catch (error) {
      console.error('Error fetching items:', error);
      res.status(500).json({ message: 'Server error while fetching items' });
    }
  });
  


module.exports = router;
