import { cache } from "react";
import "server-only";
// 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";

export interface QcItemWithChecks {
  id: number;
  code: string;
  name: string;
  itemId: number;
  lowerLimit: number | undefined;
  upperLimit: number | undefined;
  description: string | undefined;
}

export interface QcCategory {
  id: number,
  code?: string,
  name?: string,
  description?: string,
  qcItems: QcData[],
}
export interface QcData {
  id?: number,
  qcItemId: number,
  code?: string,
  name?: string,
  order?: number,
  description?: string,
  qcPassed?: boolean,
  failQty?: number,
  remarks?: string,
}
export interface QcResult extends QcData{
  id?: number;
  qcItemId: number;
  qcPassed?: boolean;
  failQty?: number;
  remarks?: string;
  escalationLogId?: number;
  stockInLineId?: number;
  stockOutLineId?: number;  
}
export interface QcInput {
  id: number;
  itemId: number;
  acceptedQty: number;
  demandQty: number;
  status?: string;

  jobOrderId: number;
  purchaseOrderId?: number;
  purchaseOrderLineId: number;
}
export interface QcFormInput {
  acceptQty: number;
  qcAccept: boolean;
  qcDecision?: number;
  qcResult: QcResult[];
}

export const fetchQcItemCheckList = cache(async () => {
  return serverFetchJson<QcItemWithChecks[]>(`${BASE_API_URL}/qc/list`, {
    next: { tags: ["qc"] },
  });
});

export const fetchQcItemCheck = cache(async (itemId?: number) => {
  let url = `${BASE_API_URL}/qcCheck`;
  if (itemId) url += `/${itemId}`;
  return serverFetchJson<QcItemWithChecks[]>(url, {
    next: { tags: ["qc"] },
  });
});
