"use server";

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

import { revalidatePath, revalidateTag } from "next/cache";
//import { QcItemResult } from '@/app/api/settings/qcItem';
import { QcItemResult } from "../../../api/settings/qcItem";

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

export interface SaveQcItemResponse {
  id?: number;
  code: string;
  name: string;
  description?: string;
  errors: Record<keyof SaveQcItemInputs, string>;
}

export const saveQcItem = async (data: SaveQcItemInputs) => {
  const response = await serverFetchJson<SaveQcItemResponse>(
    `${BASE_API_URL}/qcItems/save`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag("qcItems");

  return response;
};

export const deleteQcItem = async (id: number) => {
  const response = await serverFetchJson<QcItemResult[]>(
    `${BASE_API_URL}/qcItems/${id}`,
    {
      method: "DELETE",
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag("qcItems");
  revalidatePath("/(main)/settings/qcItem");

  return response;
};
