import returnService from '../services/returnService.js';

export const listReturns = async (req, res, next) => {
  try {
    const data = await returnService.listReturns(req.query);
    res.json({ success: true, data });
  } catch (error) {
    next(error);
  }
};

export const getReturns = async (req, res, next) => {
  try {
    const data = await returnService.getReturnsForSale(req.params.id);
    res.json({ success: true, data });
  } catch (error) {
    next(error);
  }
};

export const createReturn = async (req, res, next) => {
  try {
    const saleReturn = await returnService.createReturn(req.params.id, req.body, req);
    res.status(201).json({
      success: true,
      message: `Return ${saleReturn.returnNumber} processed. Refund: ₹${saleReturn.totalRefund}`,
      data: saleReturn,
    });
  } catch (error) {
    next(error);
  }
};
