import { api } from "src/boot/axios";
import type { PayPeriod } from "src/modules/shared/models/pay-period.models";
import type { TimesheetResponse } from "src/modules/timesheets/models/timesheet.models";
import type { TimesheetApprovalOverview } from "src/modules/timesheet-approval/models/timesheet-overview.models";
import type { BackendResponse } from "src/modules/shared/models/backend-response.models";
import type { FederalHoliday } from "src/modules/timesheets/models/federal-holidays.models";
import type { PaidTimeOff } from "src/modules/employee-list/models/employee-profile.models";

export const timesheetService = {
    getAllFederalHolidays: async (year?: number): Promise<FederalHoliday[]> => {
        const response = await api.get<{ holidays: FederalHoliday[] }>(`https://canada-holidays.ca/api/v1/holidays${year ? ('?year=' + year) : ''}`, { withCredentials: false });
        return response.data.holidays;
    },

    getPayPeriodByDate: async (date_string: string): Promise<PayPeriod> => {
        const response = await api.get<{ success: boolean, data: PayPeriod, error?: string }>(`pay-periods/date/${date_string}`);
        return response.data.data;
    },

    getPayPeriodByYearAndPeriodNumber: async (year: number, period_number: number): Promise<PayPeriod> => {
        const response = await api.get<{ success: boolean, data: PayPeriod, error?: string }>(`pay-periods/${year}/${period_number}`);
        return response.data.data;
    },

    getTimesheetOverviewsByPayPeriodAndSupervisorEmail: async (year: number, period_number: number, supervisor_email: string): Promise<TimesheetApprovalOverview[]> => {
        const response = await api.get<{ success: boolean, data: TimesheetApprovalOverview[], error?: string }>(`pay-periods/${year}/${period_number}/${supervisor_email}`);
        return response.data.data;
    },

    getTimesheetsByPayPeriodAndOptionalEmail: async (year: number, period_number: number, employee_email?: string): Promise<BackendResponse<TimesheetResponse>> => {
        if (employee_email !== undefined) {
            const response = await api.get<BackendResponse<TimesheetResponse>>(`timesheets/${year}/${period_number}?employee_email=${employee_email}`);
            return response.data;
        } else {
            const response = await api.get<BackendResponse<TimesheetResponse>>(`timesheets/${year}/${period_number}`);
            return response.data;
        }
    },

    getPaidTimeOffTotalsWithOptionalEmployeeEmail: async (email?: string): Promise<BackendResponse<PaidTimeOff>> => {
        if (email) {
            const response = await api.get<BackendResponse<PaidTimeOff>>(`paid-time-off/totals?email=${email}`);
            return response.data;
        }

        const response = await api.get<BackendResponse<PaidTimeOff>>('paid-time-off/totals');
        return response.data;
    },

    applyPresetToWeek: async (timesheet_id: number, employeeEmail?: string): Promise<BackendResponse<boolean>> => {
        if (employeeEmail) {
            const response = await api.post<BackendResponse<boolean>>(`schedule-presets/apply-preset/${employeeEmail}`, { timesheet_id });
            return response.data;
        }

        const response = await api.post<BackendResponse<boolean>>('schedule-presets/apply-preset', { timesheet_id });
        return response.data;
    },

    applyPresetToDay: async (timesheet_id: number, week_day_index: number, date: string, employeeEmail?: string): Promise<BackendResponse<boolean>> => {
        if (employeeEmail) {
            const response = await api.post<BackendResponse<boolean>>(`schedule-presets/apply-day-preset/${employeeEmail}`, { timesheet_id, week_day_index, date });
            return response.data;
        }

        const response = await api.post<BackendResponse<boolean>>('schedule-presets/apply-day-preset', { timesheet_id, week_day_index, date });
        return response.data;
    }
};