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

export interface comboProp {
  id: any;
  label: string;
}

export interface teamCombo {
  records: comboProp[];
}

export interface teamCashFlow {
  monthInvoice: string;
  invoiceMonth: string;
  income: number;
  cumulativeIncome: number;
  monthExpenditure: string;
  recordMonth: string;
  expenditure: number;
  cumulativeExpenditure: number;
}

export type CompanyTeamCashFlow = {
  month: number,
  expenditure: number,
  income: number,
  cumulativeExpenditure: number,
  cumulativeIncome: number,
}

export const fetchTeamCombo = cache(async () => {
  return serverFetchJson<teamCombo>(`${BASE_API_URL}/team/combo`);
});

export const fetchTeamCashFlowChartData = cache(async (year:number,teamId?:number,) => {
  if (teamId === null) {
    return serverFetchJson<teamCashFlow>(`${BASE_API_URL}/dashboard/searchTeamCashFlow?year=${year}`);
  } else {
    return serverFetchJson<teamCashFlow>(`${BASE_API_URL}/dashboard/searchTeamCashFlow?teamId=${teamId}&year=${year}`);
  }
});

export const fetchCompanyTeamCashFlow = cache(async (year: string, teamId: number | null,) => {
  var endpoint = `${BASE_API_URL}/dashboard/getCompanyTeamCashFlow?year=${year}`
  if (teamId) endpoint += `&teamId=${teamId}`
  return serverFetchJson<CompanyTeamCashFlow[]>(endpoint, {
    next: { tags: [year] },
  });
});

