"use server";
import { cache } from "react";
import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { IndividualTeam } from "../team";
// import "server-only";


export interface FinancialSummaryCardResult {
  teamId: number;
  teamName: string;
  projectNo: number;
  totalFee: number;
  totalBudget: number;
  cumulativeExpenditure: number;
  totalInvoiced: number;
  totalReceived: number;
  cashFlowStatus: string;
  cpi: number;
}

export type FinancialByProject = {
  id: number,
  projectName: string,
  projectCode: string,
  team: string,
  teamId: number,
  custId: number,
  customerName: string,
  customerCode: string,
  subsidiary: string,
  totalFee: number,
  totalBudget: number,
  manhourExpense: number,
  invoicedAmount: number,
  uninvoicedAmount: number,
  paidAmount: number,
  projectExpense: number,
}

export const preloadFinancialSummaryCard = () => {
  fetchFinancialSummaryCard();
};

export const fetchFinancialSummaryCard = cache(async () => {
  return serverFetchJson<FinancialSummaryCardResult[]>(`${BASE_API_URL}/dashboard/searchFinancialSummaryCard`);
});

export const fetchFinancialSummaryByProjectV2 = cache(async (teamId: number, endDate: string, startDate: string) => {
  var endpoint = `${BASE_API_URL}/dashboard/getFinancialSummary-final?`
  if (startDate.length > 0) endpoint += `&startDate=${startDate}`
  if (endDate.length > 0) endpoint += `&endDate=${endDate}`
  if (teamId > 0 ) endpoint += `&teamId=${teamId}`
  return serverFetchJson<FinancialByProject[]>(endpoint, {
    next: { tags: ["financialSummaryByProject"] },
  });
})
