/**
 * 품질관리 API 클라이언트 — wace_plm quality.xml 1:1.
 * 백엔드: backend-node/src/routes/qualityRoutes.ts
 */
import { apiClient } from "./client";

export interface IncomingRequestRow {
  objid: string;
  proposal_no?: string;
  purchase_order_no?: string;
  project_no?: string;
  product_name?: string;
  part_no?: string;
  part_name?: string;
  partner_name?: string;
  delivery_status?: string;
  request_date?: string;
  request_user_name?: string;
  inspection_yn?: string;
  request_status?: string;
}

export interface IncomingMgmtRow {
  objid: string;
  inspection_date?: string;
  inspector_name?: string;
  proposal_no?: string;
  purchase_order_no?: string;
  project_no?: string;
  product_name?: string;
  model_name?: string;
  part_no?: string;
  part_name?: string;
  partner_name?: string;
  delivery_date?: string;
  delivery_qty?: number;
  delivery_status?: string;
  inspection_qty?: number;
  defect_qty_sum?: number;
  defect_rate?: number;
  inspection_result?: string;
  inspection_file_cnt?: number;
}

export interface ProcessInspectionRow {
  objid: string;
  inspection_date?: string;
  inspector_name?: string;
  project_no?: string;
  product_name?: string;
  part_no?: string;
  part_name?: string;
  inspection_qty?: number;
  defect_qty?: number;
  work_env_status?: string;
  measuring_device?: string;
  inspection_result?: string;
  process_inspection_file_cnt?: number;
}

export interface SemiProductInspectionRow {
  objid: string;
  inspection_date?: string;
  writer_name?: string;
  product_type?: string;
  model_name?: string;
  work_order_no?: string;
  part_no?: string;
  part_name?: string;
  receipt_qty?: number;
  good_qty?: number;
  defective_qty?: number;
  defect_rate?: number;
  regeneration_qty?: number;
  final_good_qty?: number;
}

interface ListResp<T> {
  success: boolean;
  list: T[];
  pagination: { page: number; pageSize: number; total: number; totalPages: number };
}

const buildParams = (obj: Record<string, string | undefined>): Record<string, string> => {
  const p: Record<string, string> = {};
  Object.entries(obj).forEach(([k, v]) => { if (v) p[k] = v; });
  return p;
};

export const qualityApi = {
  incomingRequest: async (params: Record<string, string | undefined> = {}): Promise<ListResp<IncomingRequestRow>> =>
    (await apiClient.get("/quality/incoming-request", { params: buildParams(params) })).data,
  incomingMgmt: async (params: Record<string, string | undefined> = {}): Promise<ListResp<IncomingMgmtRow>> =>
    (await apiClient.get("/quality/incoming-mgmt", { params: buildParams(params) })).data,
  processInspection: async (params: Record<string, string | undefined> = {}): Promise<ListResp<ProcessInspectionRow>> =>
    (await apiClient.get("/quality/process-inspection", { params: buildParams(params) })).data,
  semiProductInspection: async (params: Record<string, string | undefined> = {}): Promise<ListResp<SemiProductInspectionRow>> =>
    (await apiClient.get("/quality/semi-product-inspection", { params: buildParams(params) })).data,
};
