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

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

export interface QcCategoryCombo {
  id: number;
  value: number;
  label: string;
}

export interface QcItemInfo {
  id: number;
  qcItemId: number;
  code: string;
  name?: string;
  order: number;
  description?: string;
}

export const preloadQcCategory = () => {
  fetchQcCategories();
};

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

export const fetchQcCategoryDetails = cache(async (qcCategoryId: string) => {
  return serverFetchJson<SaveQcCategoryInputs>(
    `${BASE_API_URL}/qcCategories/details/${qcCategoryId}`,
    {
      next: { tags: [`qcCategoryDetails_${qcCategoryId}`] },
    },
  );
});


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