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

export interface CashFlow {
  id: number;
  projectCode: string;
  projectName: string;
  team: string;
  teamLeader: string;
  startDate: string;
  startDateFrom: string;
  startDateFromTo: string;
  targetEndDate: string;
  client: string;
  subsidiary: string;
}

export interface CashFlowByMonthChartResult {
  monthInvoice: string;
  invoiceMonth: string;
  income: number;
  cumulativeIncome:number;
  monthExpenditure:string;
  recordMonth:string;
  expenditure:number;
  cumulativeExpenditure:number;
  incomeList: any[];
  expenditureList: any[];
  beforeCurrentYearExpenditure: any[];
  beforeCurrentYearIncome: any[];
}

export interface CashFlowReceivableAndExpenditure {
  receivedPercentage: number;
  expenditurePercentage: number;
  totalInvoiced: number;
  totalReceived: number;
  receivable: number;
  totalBudget: number;
  totalExpenditure: number;
  expenditureReceivable: number;
  totalProjectFee: number;
  invoicedPercentage: number;
  expense: number;
}
export interface CashFlowAnticipatedChartResult {
  anticipateIncomeList: any[];
  anticipateExpenditure: any[];
  monthanticipateIncome: number;
  anticipateIncomeDate: number;
  anticipateIncome:number;
  anticipateExpenditureList: any[];
  id: number;
  name: string;
  planStart: string;
  planEnd: string;
  startMonth: number;
  Duration: number;
  AverageManhours: number;
  teamLead: number;
  totalManhour: number;
  aniticipateExpenditure: number;  
}
export interface CashFlowLedgerResult {
  date: string;
  income: number;
  expenditure: number;
  balance: number;  
  remarks: string;
}

export const preloadProjects = () => {
  fetchProjectsCashFlow();
};

export const fetchProjectsCashFlow = cache(async () => {
  return serverFetchJson<CashFlow[]>(`${BASE_API_URL}/dashboard/searchCashFlowProject`);
});

export const fetchProjectsCashFlowMonthlyChart = cache(async (projectIdList: number[], year: number) => {
  if (projectIdList.length !== 0) {
    const queryParams = new URLSearchParams();
    queryParams.append('projectIdList', projectIdList.join(','));
    return serverFetchJson<CashFlowByMonthChartResult[]>(`${BASE_API_URL}/dashboard/searchCashFlowByMonth?${queryParams.toString()}&year=${year}`);
  } else {
    return [];
  }
  
});

export const fetchProjectsCashFlowReceivableAndExpenditure = cache(async (projectIdList: number[]) => {
  if (projectIdList.length !== 0) {
    const queryParams = new URLSearchParams();
    queryParams.append('projectIdList', projectIdList.join(','));
    return serverFetchJson<CashFlowReceivableAndExpenditure[]>(`${BASE_API_URL}/dashboard/searchCashFlowReceivableAndExpenditure?${queryParams.toString()}`);
  } else {
    return [];
  }
  
});

export const fetchProjectsCashFlowAnticipate = cache(async (projectIdList: number[],year:number) => {
  if (projectIdList.length !== 0) {
    const queryParams = new URLSearchParams();
    queryParams.append('projectIdList', projectIdList.join(','));
    return serverFetchJson<CashFlowAnticipatedChartResult[]>(`${BASE_API_URL}/dashboard/searchCashFlowAnticipate?${queryParams.toString()}&year=${year}`);
  } else {
    return [];
  }
  
});

export const fetchProjectsCashFlowLedger = cache(async (projectIdList: number[]) => {
  if (projectIdList.length !== 0) {
    const queryParams = new URLSearchParams();
    queryParams.append('projectIdList', projectIdList.join(','));
    return serverFetchJson<CashFlowLedgerResult[]>(`${BASE_API_URL}/dashboard/searchCashFlowLedger?${queryParams.toString()}`);
  } else {
    return [];
  }
  
});
