"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 ClientSubsidiaryProjectResult {
  color: string;
  projectId: number;
  projectCode: string;
  projectName: string;
  team: string;
  teamLead: string;
  expectedStage: string;
  budgetedManhour: number;
  spentManhour: number;
  remainedManhour: number;
  manhourConsumptionPercentage: number;
  comingPaymentMilestone: string;
}

export const fetchAllClientSubsidiaryProjects = cache(async (customerId: number, tableSorting:string, subsidiaryId?: number) => {
  if (subsidiaryId === 0){
    return serverFetchJson<ClientSubsidiaryProjectResult[]>(
      `${BASE_API_URL}/dashboard/searchCustomerSubsidiaryProject?customerId=${customerId}&tableSorting=${tableSorting}`
    );
  } else {
    return serverFetchJson<ClientSubsidiaryProjectResult[]>(
      `${BASE_API_URL}/dashboard/searchCustomerSubsidiaryProject?customerId=${customerId}&subsidiaryId=${subsidiaryId}&tableSorting=${tableSorting}`
    );
  }
  
});
