
"use server"

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

export interface CreateSkillInputs {
  id?: number;
  name: string;
  code: string;
  description: string;
}

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

export interface combo {
  records: comboProp[];
}
  
export const fetchSkillCombo = cache(async () => {
    return serverFetchJson<combo>(`${BASE_API_URL}/skill/combo`, {
      next: { tags: ["skill"] },
    });
  });

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

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