"use server";

import { serverFetchBlob, serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { Dayjs } from "dayjs";
import { cache } from "react";    
import { FileResponse } from "../reports/actions";
import { revalidateTag } from "next/cache";
import { SumOfByClient } from "@/components/ProjectFinancialSummaryV2/gptFn";
import { FinancialByProject } from ".";

export interface FinancialSummaryByClientResult {
  teamId:number;
  id:number;
  customerCode: string;
  customerName: string;
  projectNo: number;
  totalFee: number;
  totalBudget: number;
  cumulativeExpenditure: number;
  totalInvoiced: number;
  totalReceived: number;
  cashFlowStatus: string;
  cpi: number;
  totalUninvoiced: number;
}

export interface FinancialSummaryByProjectResult {
  teamId:number;
  id:number;
  projectCode: string;
  projectName: string;
  customerName: string;
  subsidiaryName: string;
  projectNo: number;
  totalFee: number;
  totalBudget: number;
  cumulativeExpenditure: number;
  totalInvoiced: number;
  totalReceived: number;
  cashFlowStatus: string;
  cpi: number;
  totalUninvoiced: number;
}

export const searchFinancialSummaryByClient = cache(async (teamId?: number) => {
    if (teamId === undefined) {
      return serverFetchJson<FinancialSummaryByClientResult[]>(
        `${BASE_API_URL}/dashboard/searchFinancialSummaryByClient`
      );
    } else {
      return serverFetchJson<FinancialSummaryByClientResult[]>(
        `${BASE_API_URL}/dashboard/searchFinancialSummaryByClient?teamId=${teamId}`
      );
    }
    
});

export const searchFinancialSummaryByProject = cache(async (teamId?: number) => {
  if (teamId === undefined) {
    return serverFetchJson<FinancialSummaryByProjectResult[]>(
      `${BASE_API_URL}/dashboard/searchFinancialSummaryByProject`
    );
  } else {
    return serverFetchJson<FinancialSummaryByProjectResult[]>(
      `${BASE_API_URL}/dashboard/searchFinancialSummaryByProject?teamId=${teamId}`
    );
  }
  
});

export const searchFinancialSummaryByProjectOld = cache(async (teamId?: number, customerId?:number) => {
  if (teamId === undefined) {
    return serverFetchJson<FinancialSummaryByProjectResult[]>(
      `${BASE_API_URL}/dashboard/searchFinancialSummaryByProject`
    );
  } else {
    return serverFetchJson<FinancialSummaryByProjectResult[]>(
      `${BASE_API_URL}/dashboard/searchFinancialSummaryByProject?teamId=${teamId}&customerId=${customerId}`
    );
  }
  
});

export interface FinancialSummaryByClientExcel {
  customerCode: string;
  customerName: string;
  projectNo: number;
  totalFee: number;
  totalBudget: number;
  // cumulativeExpenditure: number;
  totalExpenditure: number;
  manhoursExpenditure: number;
  projectExpense: number;
  totalInvoiced: number;
  totalReceived: number;
}

export interface ExportFinancialSummaryByClientExcel {
  financialSummaryByClients: FinancialSummaryByClientExcel[]
}

export interface FinancialSummaryByProjectExcel {
  projectCode: string;
  projectName: string;
  customerName: string;
  subsidiaryName?: string | null;
  totalFee: number;
  totalBudget: number;
  // cumulativeExpenditure: number;
  totalExpenditure: number;
  manhoursExpenditure: number;
  projectExpense: number;
  totalInvoiced: number;
  totalReceived: number;
}

export interface ExportFinancialSummaryByProjectExcel {
  financialSummaryByProjects: FinancialSummaryByProjectExcel[]
}

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

  return reportBlob
}) 

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

  return reportBlob
})

export const exportFinancialSummaryV2ByClientExcel = cache(async (data: SumOfByClient[]) => {
  const reportBlob = await serverFetchBlob<FileResponse>(
    `${BASE_API_URL}/dashboard/exportFinancialSummaryV2ByClientExcel`,
    {
        method: "POST",
        body: JSON.stringify(data),
        headers: { "Content-Type": "application/json" },
    },
);

  return reportBlob
}) 

export const exportFinancialSummaryV2ByProjectExcel = cache(async (data: FinancialByProject[]) => {
  const reportBlob = await serverFetchBlob<FileResponse>(
    `${BASE_API_URL}/dashboard/exportFinancialSummaryV2ByProjectExcel`,
    {
        method: "POST",
        body: JSON.stringify(data),
        headers: { "Content-Type": "application/json" },
    },
);

  return reportBlob
})

export const revalidate = async(tag: string) => {
  revalidateTag(tag)
}
