"use server";
import { serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import { TeamResult } from ".";
import { revalidateTag } from "next/cache";


export interface CreateTeamInputs {
  id: number | null;
  name: string;
  code: string;
  // team: string;
  // staffId: string;
  // grade: string;
  // joinPosition: string;
  // currentPosition: string;
  description: String;
  addStaffIds: number[];
  deleteStaffIds?: number[];
}

export interface Team {
  id: number;
  name: string;
}

export interface staff {
  id: number;
  name: string;
  team: Team;
}

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

export interface combo {
    records: comboProp[];
  }

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

export const saveTeam = async (data: CreateTeamInputs) => {
  const newTeam = serverFetchJson(`${BASE_API_URL}/team/save`, {
    method: "POST",
    body: JSON.stringify(data),
    headers: { "Content-Type": "application/json" },
  });
  revalidateTag("team")
  return newTeam
  };

    
export const deleteTeam = async (id: number) => {
  const newTeam = serverFetchWithNoContent(`${BASE_API_URL}/team/delete/${id}`, {
    method: "DELETE",
    headers: { "Content-Type": "application/json" },
  });
  revalidateTag("team")
  return newTeam
};
