import { WEEKLY_LIMIT_HOURS } from "src/common/utils/constants.utils";
import { HolidayService } from "src/time-and-attendance/domains/services/holiday.service";
import { CsvRow, InternalCsvRow } from "src/time-and-attendance/exports/export-csv-options.dto";

const REGULAR = 1;
const OVERTIME = 43;
const VACATION = 109;
const SICK = 105;

export const consolidateRowHoursAndAmountByType = (rows: InternalCsvRow[]): InternalCsvRow[] => {
    const map = new Map<string, InternalCsvRow>();

    for (const row of rows) {
        if (row.code === VACATION || row.code === SICK) {
            map.set(`${row.code}|${row.shift_date.toString()}|${row.timesheet_id}`, row);
        } else {
            const key = `${row.code}|${row.timesheet_id}`;
            if (!map.has(key)) {
                map.set(key, row);
            } else {
                const existing = map.get(key)!;
                existing.quantite_hre = (existing.quantite_hre ?? 0) + (row.quantite_hre ?? 0);
                existing.montant = (existing.montant ?? 0) + (row.montant ?? 0);
            }
        }
    }
    return Array.from(map.values());
}

export const applyHolidayRequalifications = async (
    rows: InternalCsvRow[],
    holiday_service: HolidayService,
    holiday_bank_code: string,
): Promise<InternalCsvRow[]> => {
    const result: InternalCsvRow[] = [];
    const HOLIDAY_BANK_CODE = Number(holiday_bank_code.slice(1,));

    for (const row of rows) {
        if (row.code !== HOLIDAY_BANK_CODE) {
            result.push(row);
            continue;
        }
        if (!row.premier_jour_absence || !row.dernier_jour_absence || !row.employee_matricule || !row.compagnie_no) {
            result.push(row);
            continue;
        }

        const calculated = await holiday_service.calculateHolidayPay(row.employee_matricule, row.compagnie_no, new Date(row.premier_jour_absence));

        if (!calculated.success) {
            result.push({ ...row, quantite_hre: 0 });
            continue;
        }

        result.push({ ...row, quantite_hre: calculated.data });
    }
    return result;
}

export const applyOvertimeRequalifications = (
    consolidated_rows: InternalCsvRow[],
): CsvRow[] => {
    const result: InternalCsvRow[] = [];

    //regroup rows by timesheet and week number
    const grouped_rows = new Map<string, InternalCsvRow[]>();
    for (const row of consolidated_rows) {
        const key = `${row.timesheet_id}|${row.semaine_no}`;
        if (!grouped_rows.has(key)) {
            grouped_rows.set(key, []);
        }
        grouped_rows.get(key)!.push({ ...row });
    }

    for (const [, rows] of grouped_rows) {
        const regular_hours = rows.find(r => r.code === REGULAR);

        // if no regular hours row, push as is
        if (!regular_hours?.quantite_hre) { result.push(...rows); continue; }

        // calculate overtime directly from consolidated regular hours
        const overtime_hours = Math.max(0, regular_hours.quantite_hre - WEEKLY_LIMIT_HOURS);

        // if no overtime, push as is
        if (overtime_hours <= 0) { result.push(...rows); continue; }

        // ensures that its not possible to deduct more hours than the amount of regular hours
        const deducted = Math.min(overtime_hours, regular_hours.quantite_hre);
        const remaining = regular_hours.quantite_hre - deducted;

        for (const row of rows) {
            if (row === regular_hours) {
                // pushes the regular row with subtracted overtime hours, if enough hours remaining
                if (remaining > 0) {
                    result.push({ ...regular_hours, quantite_hre: remaining });
                }
            } else {
                // other rows are left unchanged
                result.push(row);
            }
        }
        //adds a new row with overtime hours deducted from the regular hours
        result.push({
            ...regular_hours,
            code: OVERTIME,
            quantite_hre: deducted,
        });
    }
    return result;
}

export const resolveCompanyCode = (company: 'Targo' | 'Solucom'): number => {
    return company === 'Targo' ? 271583 : 271585;
}

export const computeWeekNumber = (start: Date, date: Date): number => {
    const dayMS = 86400000;
    const days = Math.floor((toUTC(date).getTime() - toUTC(start).getTime()) / dayMS);
    return Math.floor(days / 7) + 1;
}

export const toUTC = (date: Date) => {
    return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
}

export const formatDate = (d: Date): string => {
    return d.toISOString().split('T')[0];
}