const Payment = require('../models/sales');

exports.getreport = async (req, res) => {
  try {
    const { startDate, endDate } = req.query;
    const filter = {};

    if (startDate && endDate) {
      filter.paymentDate = {
        $gte: new Date(startDate),
        $lte: new Date(endDate),
      };
    }

    const payments = await Payment.find(filter).populate('student_FK');
    const total = payments.reduce(
      (sum, payment) => sum + parseFloat(payment.amount || 0),
      0
    );

    res.status(200).json({ payments, total });
  } catch (error) {
    res.status(500).json({ msg: 'Failed to fetch report', error: error.message });
  }
};
