"use server";

import { serverFetchBlob } from "@/app/utils/fetchUtil";
import { MonthlyWorkHoursReportRequest, ProjectCashFlowReportRequest, LateStartReportRequest, ProjectResourceOverconsumptionReportRequest, ProjectPandLReportRequest, ProjectCompletionReportRequest, ProjectPotentialDelayReportRequest, CostAndExpenseReportRequest, CrossTeamChargeReportRequest, ProjectManhourSummaryReportRequest, ProjectMonthlyReportRequest, LastModifiedReportRequest, ExportCurrentStaffInfoRequest } from ".";
import { BASE_API_URL } from "@/config/api";

export interface FileResponse {
    filename: string;
    blobValue: Uint8Array;
}

export const fetchProjectCashFlowReport = async (data: ProjectCashFlowReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/ProjectCashFlowReport`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchProjectPotentialDelayReport = async (data: ProjectPotentialDelayReportRequest) => {
    console.log(data)
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/ProjectPotentialDelayReport`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchMonthlyWorkHoursReport = async (data: MonthlyWorkHoursReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/StaffMonthlyWorkHourAnalysisReport`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchProjectResourceOverconsumptionReport = async (data: ProjectResourceOverconsumptionReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/ProjectResourceOverconsumptionReport`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchProjectCompletionReport = async (data: ProjectCompletionReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/ProjectCompletionReportwithOutstandingAccountsReceivable`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

// export const fetchLateStartReport = async (data: LateStartReportRequest) => {
//     const response  = await serverFetchBlob<FileResponse>(
//         `${BASE_API_URL}/reports/downloadLateStartReport`,
//         {
//             method: "POST",
//             body: JSON.stringify(data),
//             headers: { "Content-Type": "application/json" },
//         },
//     );

//     return response;
// };

export const fetchLateStartReport = async (data: LateStartReportRequest) => {
    const response = await serverFetchBlob<FileResponse>(`${BASE_API_URL}/reports/downloadLateStartReport`, {
        method: "POST",
        body: JSON.stringify(data),
        headers: { "Content-Type": "application/json" },
    });
    return response
};

export const fetchProjectPandLReport = async (data: ProjectPandLReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/projectpandlreport`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchCostAndExpenseReport = async (data: CostAndExpenseReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/costandexpenseReport`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchCrossTeamChargeReport = async (data: CrossTeamChargeReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/CrossTeamChargeReport`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchProjectManhourSummaryReport = async (data: ProjectManhourSummaryReportRequest) => {
    console.log(data)
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/ProjectManhourSummary`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};


export const fetchProjectMonthlyReport = async (data: ProjectMonthlyReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/gen-project-monthly-report`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchLastModifiedReport = async (data: LastModifiedReportRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/gen-staff-last-record-report`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};

export const fetchStaffCurrentInfo = async (data: ExportCurrentStaffInfoRequest) => {
    const reportBlob = await serverFetchBlob<FileResponse>(
        `${BASE_API_URL}/reports/export-staff-current-info`,
        {
            method: "POST",
            body: JSON.stringify(data),
            headers: { "Content-Type": "application/json" },
        },
    );

    return reportBlob
};
