import { Injectable } from "@nestjs/common";
import { CsvRow } from "src/time-and-attendance/exports/export-csv-options.dto";

@Injectable()
export class CsvGeneratorService {
    //csv builder and "mise en page"
    generateCsv(
        rows: CsvRow[]
    ): Buffer {
        const body = rows.map(row => {
            const quantity_hours = (typeof row.quantite_hre === 'number') ? row.quantite_hre.toFixed(2) : '';
            const amount = (typeof row.montant === 'number' && row.montant !== 0) ? row.montant.toFixed(2) : '';
            return [
                row.compagnie_no,
                row.employee_matricule,
                row.releve ?? '',
                row.type_transaction,
                row.code,
                quantity_hours,
                row.taux_horaire ?? '',
                amount,
                row.semaine_no,
                row.division_no ?? '',
                row.service_no ?? '',
                row.departem_no ?? '',
                row.sous_departem_no ?? '',
                row.date_transaction,
                row.premier_jour_absence ?? '',
                row.dernier_jour_absence ?? '',
            ].join(';');
        }).join('\n');
        return Buffer.from('\uFEFF' + body, 'utf8');
    }
}