import { apiClient } from "./client";

// ============================================================
// 생산관리 4개 메뉴 — wace productionplanning.xml 1:1
//   1) 생산계획&실적관리 (일반)   GET  /api/production/plan-result/list
//   2) 생산계획&실적관리 (장비)   GET  /api/production/plan-result/list-equip
//   3) 반제품 소요량               POST /api/production/mbom-requirement/semi
//   4) 원자재 소요량               POST /api/production/mbom-requirement/raw
// ============================================================

export interface ProdPlanResultFilter {
  search_project_nos?: string; // multiple — "OBJID,OBJID,..."
  search_product_code?: string;
  search_category_code?: string;
  search_production_type?: string;
  search_customer_objid?: string;
  search_req_del_date_from?: string;
  search_req_del_date_to?: string;
  search_part_no?: string;
  search_part_name?: string;
  search_serial_no?: string;
  search_writer?: string;
  search_regdate_from?: string;
  search_regdate_to?: string;
  page?: number;
  page_size?: number;
}

export interface ProdPlanResultRow {
  objid: string;
  project_no: string | null;
  product: string | null;
  product_name: string | null;
  category_code: string | null;
  category_code_name: string | null;
  production_type: string | null;
  production_type_name: string | null;
  customer_objid: string | null;
  customer_name: string | null;
  req_del_date: string | null;
  customer_request: string | null;
  part_no: string | null;
  part_name: string | null;
  serial_no: string | null;
  serial_no_list: string | null;
  quantity: number | string | null;
  extra_prod_qty: number | string | null;
  total_prod_qty: number | string | null;
  assembly_qty: number | string | null;
  inspection_qty: number | string | null;
  ship_wait_qty: number | string | null;
  prod_plan_objid: string | null;
  writer: string | null;
  writer_name: string | null;
  regdate_title: string | null;
}

export interface ProdPlanResultEquipRow {
  objid: string;
  project_no: string | null;
  product: string | null;
  product_name: string | null;
  category_code: string | null;
  category_code_name: string | null;
  production_type: string | null;
  production_type_name: string | null;
  customer_objid: string | null;
  customer_name: string | null;
  req_del_date: string | null;
  customer_request: string | null;
  part_no: string | null;
  part_name: string | null;
  serial_no: string | null;
  prod_wbs_cnt: number | null;
  prod_progress_rate: number | string | null;
  delv_wbs_cnt: number | null;
  delv_progress_rate: number | string | null;
}

export interface ProdPlanResultListResponse<T> {
  rows: T[];
  totalCount: number;
  page: number;
  pageSize: number;
}

export interface ProjectOption {
  code: string;
  label: string;
}

export interface WriterOption {
  code: string;
  label: string;
}

export const prodPlanResultApi = {
  async list(filter: ProdPlanResultFilter = {}): Promise<ProdPlanResultListResponse<ProdPlanResultRow>> {
    const res = await apiClient.get("/production/plan-result/list", { params: filter });
    return res.data?.data as ProdPlanResultListResponse<ProdPlanResultRow>;
  },
  async listEquip(filter: ProdPlanResultFilter = {}): Promise<ProdPlanResultListResponse<ProdPlanResultEquipRow>> {
    const res = await apiClient.get("/production/plan-result/list-equip", { params: filter });
    return res.data?.data as ProdPlanResultListResponse<ProdPlanResultEquipRow>;
  },
  async getProjectOptions(): Promise<ProjectOption[]> {
    const res = await apiClient.get("/production/plan-result/project-options");
    return (res.data?.data ?? []) as ProjectOption[];
  },
  async getWriterOptions(): Promise<WriterOption[]> {
    const res = await apiClient.get("/production/plan-result/writer-options");
    return (res.data?.data ?? []) as WriterOption[];
  },
};

// ─── 반제품/원자재 소요량 ───────────────────────────────────

export interface MbomOption {
  objid: string;
  mbom_no: string;
  part_name: string;
}

export interface MbomRequirementInputItem {
  mbomObjid: string;
  qty: number;
}

export interface SemiRequirementRow {
  PART_NO: string;
  PART_NAME: string;
  CATEGORY_NAME: string;
  UNIT: string;
  MATERIAL: string;
  SPEC: string;
  REQUIRED_QTY: number;
}

export interface RawRequirementRow {
  PART_NO: string;
  PART_NAME: string;
  CATEGORY_NAME: string;
  UNIT: string;
  MATERIAL: string;
  SPEC: string;
  REQUIRED_QTY: number | string;
  RAW_MATERIAL: string;
  RAW_MATERIAL_SIZE: string;
  MATERIAL_PART_NO: string;
  MATERIAL_REQUIRED_QTY: number | string;
}

export const mbomRequirementApi = {
  async getOptions(): Promise<MbomOption[]> {
    const res = await apiClient.get("/production/mbom-requirement/options");
    return (res.data?.data ?? []) as MbomOption[];
  },
  async getSemi(items: MbomRequirementInputItem[]): Promise<SemiRequirementRow[]> {
    const res = await apiClient.post("/production/mbom-requirement/semi", { mbomItems: items });
    return (res.data?.data ?? []) as SemiRequirementRow[];
  },
  async getRaw(items: MbomRequirementInputItem[]): Promise<RawRequirementRow[]> {
    const res = await apiClient.post("/production/mbom-requirement/raw", { mbomItems: items });
    return (res.data?.data ?? []) as RawRequirementRow[];
  },
};
