import { apiClient } from "./client";

// ============================================================
// 개발관리 설계변경 리스트 (M5, read-only) — wace partMngHistList 1:1
// 라우트: /api/development/eo-history/*
// ============================================================

export interface EoHistoryListFilter {
  Year?: string;
  contract_objid?: string;
  unit_code?: string;
  part_no?: string;
  part_name?: string;
  change_option?: string;
  eo_start_date?: string;
  eo_end_date?: string;
  change_type?: string;
  part_type?: string;
  writer_id?: string;
  page?: number;
  page_size?: number;
}

export interface EoHistoryRow {
  objid: string;
  eo_no: string | null;
  year: string | null;
  project_no: string | null;
  project_name: string | null;
  unit_name: string | null;
  parent_part_info: string | null;
  part_no_disp: string | null;
  part_name_disp: string | null;
  part_no: string | null;
  part_name: string | null;
  bom_qty_status: string | null;
  qty: string | null;
  qty_temp: string | null;
  change_type: string | null;
  change_type_name: string | null;
  change_option: string | null;
  change_option_name: string | null;
  revision_disp: string | null;
  revision: string | null;
  eo_date: string | null;
  part_type: string | null;
  part_type_name: string | null;
  writer: string | null;
  writer_name: string | null;
  his_reg_date_title: string | null;
  bom_deploy_date: string | null;
  bom_deploy_date_title: string | null;
}

export interface EoHistoryListResponse {
  rows: EoHistoryRow[];
  total: number;
  page: number;
  pageSize: number;
}

export interface EoHistoryDetail extends EoHistoryRow {
  // raw PART_MNG_HISTORY 추가 필드
  product_mgmt_objid?: string | null;
  upg_no?: string | null;
  unit?: string | null;
  spec?: string | null;
  material?: string | null;
  weight?: string | null;
  remark?: string | null;
  es_spec?: string | null;
  ms_spec?: string | null;
  design_apply_point?: string | null;
  management_flag?: string | null;
  status?: string | null;
  reg_date?: string | null;
  is_last?: string | null;
  sourcing_code?: string | null;
  sub_material?: string | null;
  thickness?: string | null;
  width?: string | null;
  height?: string | null;
  out_diameter?: string | null;
  in_diameter?: string | null;
  length?: string | null;
  supply_code?: string | null;
  contract_objid?: string | null;
  maker?: string | null;
  his_status?: string | null;
  bom_status?: string | null;
  heat_treatment_hardness?: string | null;
  heat_treatment_method?: string | null;
  surface_treatment?: string | null;
  customer_project_name?: string | null;
}

export const devEoHistoryApi = {
  async list(filter: EoHistoryListFilter = {}): Promise<EoHistoryListResponse> {
    const res = await apiClient.get("/development/eo-history/list", { params: filter });
    return res.data?.data as EoHistoryListResponse;
  },
  async detail(objid: string): Promise<EoHistoryDetail | null> {
    const res = await apiClient.get(`/development/eo-history/${objid}`);
    return res.data?.data ?? null;
  },
};
