import {instance} from "./axios";
import {
    AlfaEna,
    AlfaEnaFilters,
    AlfaEnaMonth,
    AlfaEnaResponse,
    GetAlfaEnaParams,
    TimesheetMonth
} from "../models/timesheets";
import {CustomSelectOptions} from "../models/ReactComponents";
import {RecordsResponse} from "../models/Axios";
import {AxiosResponse} from "axios";
import {GetDeltiaResponse} from "../models/HrSupport/PersonalFolder";
import {InitedAvailabYearsData} from "../components/Difo/Aena/AenaSingle/AenaHeader";

export async function getTimesheetMonth(month: string) {

    return instance.get<{ "timesheet_month": TimesheetMonth }>("api/timesheets/month", {
        params: {month: month},
    });
}


export async function getHolidaysLookup() {

    return instance.get("api/timesheets/holiday/codes");
}

export async function postTimeSheet(params: any) {

    return instance.post("api/timesheets/load/excel", params, {responseType: 'arraybuffer'});
}


export async function lockYear(
    year: string | number | undefined,
    locked: boolean,
    hashId: string,
    activeRole: string
) {
    return instance.post("api/hr_support/v1/timesheet/lock/a1/excel", {

        hash_id: hashId,
        year: year,
        locked: locked,
        active_role: activeRole
    });
}


export async function getNavigatorYears(hashId: string,  activeRole:string) {
    return instance.get<any>(`api/hr_support/v1/timesheet/years/${hashId}/${activeRole}/list`);
}

export async function saveYearPeriods(payload: {
    hash_id: number;
    year: string;
    periods: { months: number[]; project_codes: string[] }[];
    active_role: string;
}) {
    return instance.post("api/hr_support/v1/timesheet/manual/initialization", payload);
}




const now = new Date();

const pad = (n: number) => String(n).padStart(2, '0');

const datePart = `${pad(now.getDate())}/${pad(now.getMonth() + 1)}/${now.getFullYear()}`;
const timePart = `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;

const last_fetched = `${datePart}|${timePart}`;

// ── API calls ──────────────────────────────────────────────────────────────

export function getAlfaEna(params: GetAlfaEnaParams) {
    return instance.post<RecordsResponse<AlfaEna>>(`api/hr_support/v1/timesheet/overview`, params);
}




export function getAlfaEnaFilters(activeRole: string, year: number) {
    return instance.get<AlfaEnaFilters>(`api/hr_support/v1/timesheet/overview/filters`, {
        params: {
            role: activeRole,
            year: year
        },
    });
}

export async function sendAlfaEnaReminder(userId: any, activeRole: string, month: number, year: number, project_code?: any) {
    return instance.post(`api/hr_support/v1/timesheet/overview/remind`, {
        user_id: userId,
        role: activeRole,
        month: month,
        year: year,
        project_code
    });
}



export async function getDeltia(
    hashId: string,
    year: number | string,
    activeRole: string,
    signal?: AbortSignal   // 👈 add this
) {
    return instance.get<GetDeltiaResponse>(
        `api/hr_support/v1/timesheet/year/view/${hashId}/${year}/${activeRole}`,
        { signal }
    );
}


export async function uploada1Pdf(formDataObject:any) {

    return instance.post("api/hr_support/v1/timesheet/upload/a1/month/pdf", formDataObject);
}

export async function deletea1Pdf(uuid: string) {
    const form = new FormData();
    form.append('uuid', uuid);
    return instance.post(`api/hr_support/v1/timesheet/delete/a1/month/pdf`, form);
}

export async function locka1Pdf(hashId: string, year: string, month: number, activeRole:string, locked:boolean) {

    return instance.post("api/hr_support/v1/timesheet/lock/a1/pdf", {
locked: locked,
        hash_id: hashId,
        year: year,
        month: month,
active_role: activeRole
    });
}


export async function uploadAlfaEnaFile(formData: FormData) {
    return instance.post(`api/hr_support/v1/timesheet/upload/a1/year/excel`, formData);
}


export async function downloadLatestExcelA1(formData: FormData) {
    return instance.post(`api/hr_support/v1/timesheet/download/a1`, formData, {
        responseType: 'blob', // 👈
    });
}


export async function getSkonakiData(year: string, userHashId: number, activeRole:string) {
    return instance.post<any>("api/hr_support/v1/timesheet/useful/info", {
            year: year,
            hash_id: userHashId,
        active_role:activeRole
    });
}

export interface GetRescomYearsResponse {
    data: InitedAvailabYearsData[];
}

export async function getInitdAvailabYearsData(userHashId: number, activeRole: string) {
        return await instance.get<GetRescomYearsResponse>(
            `api/hr_support/v1/timesheet/synced/years-initialization/${userHashId}/${activeRole}/info`,
        );
}

export async function removeA1Year(hashId: string, year: string, activeRole:string) {
    return instance.post(`api/hr_support/v1/timesheet/delete/year/manual/${year}/${hashId}/${activeRole}`);

}


export async function addSelectedYears(payload: {
    active_role: string;
    hash_id: string;
    years: string[];
}) {
    return instance.post(`api/hr_support/v1/timesheet/synced/years-initialization/save`, payload);
}
