const express = require('express');
const router = express.Router();
const TransactionDetail = require('../models/TransactionDetail');
const transactionDetailController = require('../controllers/transactionDetailController');

// Route to add transaction details (items to a transaction)
router.post('/', transactionDetailController.addTransactionDetails);

// Route to get transaction details by transactionId
router.get('/:transactionId', transactionDetailController.getTransactionDetails);

router.put('/:id', async (req, res) => {
    const detailId = req.params.id;
    const { name, quantity, price, total } = req.body;
  
// stock cheking
// profit and related updated shall be considred


    try {
      await TransactionDetail.findByIdAndUpdate(detailId, {
        name,
        quantity,
        price,
        total
      });
      res.json({ success: true });
    } catch (error) {
      res.status(500).json({ error: 'Failed to update transaction detail' });
    }
  });
  


router.delete('/transactionDetails/:id', async (req, res) => {
    const detailId = req.params.id;
    try {
      await TransactionDetail.findByIdAndDelete(detailId);
      res.json({ success: true });
    } catch (error) {
      res.status(500).json({ error: 'Failed to delete transaction detail' });
    }
  });
  


module.exports = router;
