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

export interface TeamResult {
  action: any;
  id: number;
  teamId: number;
  name: string;
  code: string;
  description: string;
  staffId: string;
  staffName: string;
  posLabel: string;
  posCode: string;
  teamLead: number;
}

export type IndivTeam = {
  team: IndividualTeam
  staffIds: number[]
}

export type IndividualTeam = {
  id: number;
  description: string;
  name: string;
  code: string;
  teamLead: IndividualStaff;
  staff: IndividualStaff;
}

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

export interface combo {
  records: comboProp[];
}

export const fetchTeam = cache(async () => {
  return serverFetchJson<TeamResult[]>(`${BASE_API_URL}/team`, {
    next: { tags: ["team"] },
  });
});

export const fetchIndivTeam = cache(async (id: number) => {
  return serverFetchJson<IndivTeam>(`${BASE_API_URL}/team/${id}`, {
    next: { tags: ["team"] },
  });
});

export const preloadTeamDetail = () => {
  fetchTeamDetail();
};

export const fetchTeamDetail = cache(async () => {
  return serverFetchJson<TeamResult[]>(`${BASE_API_URL}/team/detail`, {
    next: { tags: ["team"] },
  });
});

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