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

export type BomStatus = "active" | "inactive";

export interface BomCombo {
  id: number;
  value: number;
  label: string;
  outputQty: number;
  outputQtyUom: string;
  description: string;
  status?: BomStatus;
}

export type BomComboIssueCode =
  | "MISSING_BOM_CODE"
  | "MISSING_BOM_NAME"
  | "MISSING_ITEM"
  | "MISSING_SALES_UOM"
  | "MISSING_UOM_CONVERSION"
  | "MISSING_STOCK_UOM"
  | "MISSING_STOCK_UOM_CONVERSION";

export interface BomComboIssue {
  bomId: number;
  bomCode: string | null;
  bomName: string | null;
  itemId: number | null;
  description: string | null;
  issueCode: BomComboIssueCode;
}

export interface BomFormatFileGroup {
    fileName: string;
    problems: string[];
}

/** Format-check 回傳：正確檔名列表 + 失敗列表 */
export interface BomFormatCheckResponse {
    correctFileNames: string[];
    failList: BomFormatFileGroup[];
    issueLogFileId: string;
}

export interface BomUploadResponse {
    batchId: string;
    fileNames: string[];
}

export interface ImportBomItemPayload {
    fileName: string;
    isAlsoWip: boolean;
    isDrink: boolean;
    isPowderMixture: boolean;
}

export const preloadBomCombo = (() => {
    fetchBomCombo()
})
export interface BomScoreResult {
  id: number;
  code: string;
  name: string;
  baseScore: number | string | { value?: number; [key: string]: any };
}



export const fetchBomCombo = cache(async () => {
  return serverFetchJson<BomCombo[]>(`${BASE_API_URL}/bom/combo`, {
    next: { tags: ["bomCombo"] },
  });
});

export const fetchBomScores = cache(async () => {
  return serverFetchJson<BomScoreResult[]>(`${BASE_API_URL}/bom/scores`, {
    next: { tags: ["boms"] },
  });
});

export interface BomMaterialDto {
  itemCode?: string;
  itemName?: string;
  isConsumable?: boolean;
  baseQty?: number;
  baseUom?: string;
  stockQty?: number;
  stockUom?: string;
  salesQty?: number;
  salesUom?: string;
}

export interface BomProcessDto {
  seqNo?: number;
  processCode?: string;
  processName?: string;
  processDescription?: string;
  equipmentCode?: string;
  equipmentName?: string;
  durationInMinute?: number;
  prepTimeInMinute?: number;
  postProdTimeInMinute?: number;
}

export interface BomDetailResponse {
  id: number;
  itemCode?: string;
  itemName?: string;
  isDark?: number;
  isFloat?: number;
  isDense?: number;
  isDrink?: boolean;
  isPowderMixture?: boolean;
  scrapRate?: number;
  allergicSubstances?: number;
  timeSequence?: number;
  complexity?: number;
  baseScore?: number;
  description?: string;
  outputQty?: number;
  outputQtyUom?: string;
  status?: BomStatus;
  materials: BomMaterialDto[];
  processes: BomProcessDto[];
}

export interface EditBomRequest {
  // basic fields
  description?: string;
  outputQty?: number;
  outputQtyUom?: string;
  yield?: number;

  // baseScore inputs (server will recalculate)
  isDark?: number;
  isFloat?: number;
  isDense?: number;
  scrapRate?: number;
  allergicSubstances?: number;
  timeSequence?: number;
  complexity?: number;
  isDrink?: boolean;
  isPowderMixture?: boolean;
  status?: BomStatus;

  materials?: EditBomMaterialRequest[];
  processes?: EditBomProcessRequest[];
}

export interface EditBomMaterialRequest {
  id?: number;
  // At least one of itemId/itemCode should be present.
  itemId?: number;
  itemCode?: string;
  qty: number;
  isConsumable?: boolean;
}

export interface EditBomProcessRequest {
  id?: number;
  seqNo?: number;
  processId?: number;
  processCode?: string;
  equipmentId?: number;
  equipmentCode?: string;
  newEquipment?: {
    code: string;
    name: string;
    description?: string;
    equipmentTypeId?: number;
  };
  description?: string;
  durationInMinute?: number;
  prepTimeInMinute?: number;
  postProdTimeInMinute?: number;
}