const PaymentDetail = require('../models/PaymentDetail'); // Adjust the path as needed

// Create a new Payment Detail
exports.createPaymentDetail = async (req, res) => {
  try {
    const { cash, bank, credit, other, description } = req.body;

    const newPaymentDetail = new PaymentDetail({
      cash,
      bank,
      credit,
      other,
      description,
    });

    const savedPaymentDetail = await newPaymentDetail.save();
    res.status(201).json({
      message: 'Payment detail created successfully',
      paymentDetail: savedPaymentDetail,
    });
  } catch (error) {
    console.error('Error creating payment detail:', error);
    res.status(500).json({ message: 'Error creating payment detail', error: error.message });
  }
};

// Get all Payment Details
exports.getAllPaymentDetails = async (req, res) => {
  try {
    const paymentDetails = await PaymentDetail.find();
    res.status(200).json(paymentDetails);
  } catch (error) {
    console.error('Error fetching payment details:', error);
    res.status(500).json({ message: 'Error fetching payment details', error: error.message });
  }
};

// Get a single Payment Detail by ID
exports.getPaymentDetailById = async (req, res) => {
  try {
    const { id } = req.params;
    const paymentDetail = await PaymentDetail.findById(id);

    if (!paymentDetail) {
      return res.status(404).json({ message: 'Payment detail not found' });
    }

    res.status(200).json(paymentDetail);
  } catch (error) {
    console.error('Error fetching payment detail:', error);
    res.status(500).json({ message: 'Error fetching payment detail', error: error.message });
  }
};

// Update a Payment Detail by ID
exports.updatePaymentDetail = async (req, res) => {
  try {
    const { id } = req.params;
    const { cash, bank, credit, other, description } = req.body;

    const updatedPaymentDetail = await PaymentDetail.findByIdAndUpdate(
      id,
      { cash, bank, credit, other, description },
      { new: true, runValidators: true }
    );

    if (!updatedPaymentDetail) {
      return res.status(404).json({ message: 'Payment detail not found' });
    }

    res.status(200).json({
      message: 'Payment detail updated successfully',
      paymentDetail: updatedPaymentDetail,
    });
  } catch (error) {
    console.error('Error updating payment detail:', error);
    res.status(500).json({ message: 'Error updating payment detail', error: error.message });
  }
};

// Delete a Payment Detail by ID
exports.deletePaymentDetail = async (req, res) => {
  try {
    const { id } = req.params;

    const deletedPaymentDetail = await PaymentDetail.findByIdAndDelete(id);

    if (!deletedPaymentDetail) {
      return res.status(404).json({ message: 'Payment detail not found' });
    }

    res.status(200).json({ message: 'Payment detail deleted successfully' });
  } catch (error) {
    console.error('Error deleting payment detail:', error);
    res.status(500).json({ message: 'Error deleting payment detail', error: error.message });
  }
};
