const ExcelJS = require('exceljs');

exports.generateExcel = async (res, title, headers, data) => {
  const workbook = new ExcelJS.Workbook();
  const sheet = workbook.addWorksheet(title);

  sheet.columns = headers.map(h => ({ header: h, key: h }));
  data.forEach(row => sheet.addRow(row));

  res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  res.setHeader('Content-Disposition', `attachment; filename=${title}.xlsx`);
  await workbook.xlsx.write(res);
  res.end();
};
