const TransactionDetail = require('../models/TransactionDetail');

// Add transaction details (items to a transaction)
exports.addTransactionDetails = async (req, res) => {
  try {
    const { transactionId, items } = req.body;

    // Create the transaction details (items) and associate them with the transaction
    const transactionDetails = items.map(item => ({
      transactionId: transactionId,
      itemId: item.id,
      quantity: item.quantity,
      price: item.price
    }));

    await TransactionDetail.insertMany(transactionDetails);
    res.status(201).json({ message: 'Transaction details added successfully!' });
  } catch (error) {
    res.status(500).json({ message: 'Error adding transaction details', error });
  }
};

// Get transaction details by transactionId (for example)
exports.getTransactionDetails = async (req, res) => {
  try {
  
    const { transactionId } = req.params;
 
    const details = await TransactionDetail.find({ transactionId });


    res.status(200).json(details);
  } catch (error) {
    res.status(500).json({ message: 'Error retrieving transaction details', error });
  }
};
