import ExcelJS from "exceljs";

export const generateAttendanceExcel = async (records: any[]) => {
  const workbook = new ExcelJS.Workbook();
  const sheet = workbook.addWorksheet("Attendance");

  sheet.columns = [
    { header: "S.No.", key: "serialNo", width: 8 },
    { header: "Date", key: "date", width: 14 },
    { header: "Employee Code", key: "employeeCode", width: 16 },
    { header: "Employee Name", key: "employeeName", width: 25 },
    { header: "Status", key: "status", width: 15 },
    { header: "Remarks", key: "remarks", width: 30 },
  ];

  const headerRow = sheet.getRow(1);
  headerRow.font = { bold: true, color: { argb: "FFFFFFFF" } };
  headerRow.fill = {
    type: "pattern",
    pattern: "solid",
    fgColor: { argb: "FF0F5132" },
  };
  headerRow.alignment = { horizontal: "center", vertical: "middle" };

  records.forEach((rec, index) => {
    sheet.addRow({
      serialNo: index + 1,
      date: rec.date,
      employeeCode: rec.employeeCode,
      employeeName: rec.employeeName,
      status: rec.status,
      remarks: rec.remarks ?? "",
    });
  });

  sheet.eachRow((row) => {
    row.eachCell((cell) => {
      cell.border = {
        top: { style: "thin" },
        left: { style: "thin" },
        bottom: { style: "thin" },
        right: { style: "thin" },
      };
    });
  });

  return workbook;
};
