"use server";

import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { Dayjs } from "dayjs";
import { cache } from "react";    


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;
  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, 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}`
    );
  }
  
});
