import { apiClient } from "./client";

// wace projectMgmtWbsGridList 매퍼 + 영업관리 패턴 통일
export interface ProgressListFilter {
  Year?: string;
  project_nos?: string;            // OBJID (단일 또는 쉼표 다중)
  category_cd?: string;
  customer_objid?: string;
  product?: string;
  status_cd?: string;
  result_cd?: string;
  contract_start_date?: string;
  contract_end_date?: string;
  area_cd?: string;                // '국내'/'해외'
  free_of_charge?: string;         // '유상'/'무상'
  // 영업관리 패턴 통일 — PartSelect의 value (part_mng.objid::varchar)
  search_partObjId?: string;
  serial_no?: string;
  pm_user_id?: string;
  location?: string;
  setup?: string;
}

export interface ProjectNoOption {
  value: string;
  label: string;
}

// 운영판 그리드 18셀 + 부속 필드
export interface ProgressRow {
  objid: string;
  project_no: string | null;
  project_name: string | null;
  category_cd: string | null;
  category_name: string | null;
  customer_objid: string | null;
  customer_name: string | null;
  product: string | null;
  product_name: string | null;
  area_name: string | null;
  reg_date: string | null;
  free_of_charge: string | null;
  product_item_code: string | null;
  product_item_name: string | null;
  serial_no: string | null;
  contract_qty: number | string | null;
  req_del_date: string | null;
  ebom_status: string | null;
  mbom_status: string | null;
  order_date: string | null;
  receiving_rate: string | null;
  production_team_12: string | null;
  production_team_3: string | null;
  assembly: string | null;
  verification: string | null;
  shipment_date: string | null;
  // 부속
  contract_objid: string | null;
  contract_no: string | null;
  overhaul_order: string | null;
  pm_user_name: string | null;
  writer_name: string | null;
  cu01_cnt: number | null;
  cu02_cnt: number | null;
  // 다이얼로그 표시용 (wace 운영판 1:1)
  contract_currency_name: string | null;
  exchange_rate: string | null;
  customer_request: string | null;
  return_reason_name: string | null;
}

export interface ProgressDetail {
  objid: string;
  project_no: string;
  project_name: string;
  facility: string;
  facility_name: string;
  facility_qty: string;
  facility_depth: string;
  contract_del_date: string;
  contract_currency: string;
  contract_currency_name: string;
  contract_price: string;
  contract_price_currency: string;
}

export interface ProgressUpdateBody {
  project_no?: string;
  project_name?: string;
  facility?: string;
  facility_qty?: string;
  facility_depth?: string;
  contract_del_date?: string;
  contract_currency?: string;
  contract_price_currency?: string;
  contract_price?: string;
}

export const projectMgmtApi = {
  async list(filter: ProgressListFilter = {}) {
    const res = await apiClient.get("/project/progress/list", { params: filter });
    return (res.data?.data ?? []) as ProgressRow[];
  },
  async projectNoOptions(): Promise<ProjectNoOption[]> {
    const res = await apiClient.get("/project/progress/project-no-options");
    return (res.data?.data ?? []) as ProjectNoOption[];
  },
  async detail(objid: string): Promise<ProgressDetail | null> {
    const res = await apiClient.get(`/project/progress/${objid}`);
    return res.data?.data ?? null;
  },
  async update(objid: string, body: ProgressUpdateBody) {
    return (await apiClient.put(`/project/progress/${objid}`, body)).data;
  },
};
