import { apiClient } from "./client";

// --- 타입 정의 ---

export interface PkgUnit {
  id: string;
  company_code: string;
  pkg_code: string;
  pkg_name: string;
  pkg_type: string;
  status: string;
  width_mm: number | null;
  length_mm: number | null;
  height_mm: number | null;
  self_weight_kg: number | null;
  max_load_kg: number | null;
  volume_l: number | null;
  remarks: string | null;
  created_date: string;
  writer: string | null;
}

export interface PkgUnitItem {
  id: string;
  company_code: string;
  pkg_code: string;
  item_number: string;
  pkg_qty: number;
  // JOIN된 필드
  item_name?: string;
  spec?: string;
  unit?: string;
}

export interface LoadingUnit {
  id: string;
  company_code: string;
  loading_code: string;
  loading_name: string;
  loading_type: string;
  status: string;
  width_mm: number | null;
  length_mm: number | null;
  height_mm: number | null;
  self_weight_kg: number | null;
  max_load_kg: number | null;
  max_stack: number | null;
  remarks: string | null;
  created_date: string;
  writer: string | null;
}

export interface LoadingUnitPkg {
  id: string;
  company_code: string;
  loading_code: string;
  pkg_code: string;
  max_load_qty: number;
  load_method: string | null;
  // JOIN된 필드
  pkg_name?: string;
  pkg_type?: string;
}

export interface ItemInfoForPkg {
  id: string;
  item_number: string;
  item_name: string;
  size: string | null;
  spec?: string | null;
  material: string | null;
  unit: string | null;
  division: string | null;
}

// --- 포장단위 API ---

export async function getPkgUnits() {
  const res = await apiClient.get("/packaging/pkg-units");
  return res.data as { success: boolean; data: PkgUnit[] };
}

export async function createPkgUnit(data: Partial<PkgUnit>) {
  const res = await apiClient.post("/packaging/pkg-units", data);
  return res.data as { success: boolean; data: PkgUnit; message?: string };
}

export async function updatePkgUnit(id: string, data: Partial<PkgUnit>) {
  const res = await apiClient.put(`/packaging/pkg-units/${id}`, data);
  return res.data as { success: boolean; data: PkgUnit };
}

export async function deletePkgUnit(id: string) {
  const res = await apiClient.delete(`/packaging/pkg-units/${id}`);
  return res.data as { success: boolean; message?: string };
}

// --- 포장단위 매칭품목 API ---

export async function getPkgUnitItems(pkgCode: string) {
  const res = await apiClient.get(`/packaging/pkg-unit-items/${encodeURIComponent(pkgCode)}`);
  return res.data as { success: boolean; data: PkgUnitItem[] };
}

export async function createPkgUnitItem(data: { pkg_code: string; item_number: string; pkg_qty: number }) {
  const res = await apiClient.post("/packaging/pkg-unit-items", data);
  return res.data as { success: boolean; data: PkgUnitItem; message?: string };
}

export async function deletePkgUnitItem(id: string) {
  const res = await apiClient.delete(`/packaging/pkg-unit-items/${id}`);
  return res.data as { success: boolean; message?: string };
}

// --- 적재함 API ---

export async function getLoadingUnits() {
  const res = await apiClient.get("/packaging/loading-units");
  return res.data as { success: boolean; data: LoadingUnit[] };
}

export async function createLoadingUnit(data: Partial<LoadingUnit>) {
  const res = await apiClient.post("/packaging/loading-units", data);
  return res.data as { success: boolean; data: LoadingUnit; message?: string };
}

export async function updateLoadingUnit(id: string, data: Partial<LoadingUnit>) {
  const res = await apiClient.put(`/packaging/loading-units/${id}`, data);
  return res.data as { success: boolean; data: LoadingUnit };
}

export async function deleteLoadingUnit(id: string) {
  const res = await apiClient.delete(`/packaging/loading-units/${id}`);
  return res.data as { success: boolean; message?: string };
}

// --- 적재함 포장구성 API ---

export async function getLoadingUnitPkgs(loadingCode: string) {
  const res = await apiClient.get(`/packaging/loading-unit-pkgs/${encodeURIComponent(loadingCode)}`);
  return res.data as { success: boolean; data: LoadingUnitPkg[] };
}

export async function createLoadingUnitPkg(data: { loading_code: string; pkg_code: string; max_load_qty: number; load_method?: string }) {
  const res = await apiClient.post("/packaging/loading-unit-pkgs", data);
  return res.data as { success: boolean; data: LoadingUnitPkg; message?: string };
}

export async function deleteLoadingUnitPkg(id: string) {
  const res = await apiClient.delete(`/packaging/loading-unit-pkgs/${id}`);
  return res.data as { success: boolean; message?: string };
}

// --- 품목정보 연동 API ---

export async function getItemsByDivision(divisionLabel: string, keyword?: string) {
  const res = await apiClient.get(`/packaging/items/${encodeURIComponent(divisionLabel)}`, {
    params: keyword ? { keyword } : {},
  });
  return res.data as { success: boolean; data: ItemInfoForPkg[] };
}

export async function getGeneralItems(keyword?: string) {
  const res = await apiClient.get("/packaging/items/general", {
    params: keyword ? { keyword } : {},
  });
  return res.data as { success: boolean; data: ItemInfoForPkg[] };
}
