"use server";

import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { TaskTemplate } from ".";
import { revalidateTag } from "next/cache";

export interface NewTaskTemplateFormInputs {
  code: string;
  name: string;
  taskIds: number[];
}

export const saveTaskTemplate = async (data: NewTaskTemplateFormInputs) => {
  const newTaskTemplate = await serverFetchJson<TaskTemplate>(
    `${BASE_API_URL}/tasks/templates/new`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag("taskTemplates");

  return newTaskTemplate;
};
