// ============================================================
// 자재관리 — 자재리스트 + 불출의뢰서 API
// 백엔드: /api/inventory-mng
// ============================================================

import { apiClient } from "./client";

export interface InventoryListFilter {
  project_objid?: string;
  unit_code?: string;
  part_no?: string;
  part_name?: string;
  part_type?: string;
  location?: string;
  cls_cd?: string;
  cau_cd?: string;
  writer?: string;
  page?: number;
  page_size?: number;
}

export interface IssueRequestFilter {
  part_no?: string;
  part_name?: string;
  request_start_date?: string;
  request_end_date?: string;
  request_user?: string;
  reception_status?: string;
  reception_user?: string;
  reception_start_date?: string;
  reception_end_date?: string;
  out_status?: string;
  page?: number;
  page_size?: number;
}

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

export interface OptionItem { code: string; label: string; part_name?: string }

export interface SaveStockInput {
  project_objid: string;
  unit?: string;
  part_objid: string;
  qty: number;
  price?: string;
  location: string;
  sub_location?: string;
  cls_cd?: string;
  cau_cd?: string;
  receipt_date?: string;
  writer?: string;
  order_objid?: string;
}

export interface MoveItem {
  in_objid: string;
  move_qty: number;
  location?: string;
  sub_location?: string;
  move_date?: string;
  move_user?: string;
}

export interface IssueRequestLineInput {
  parent_objid: string;
  request_qty: number;
  unit?: string;
}

export interface IssueRequestSaveInput {
  master_objid?: string;
  contract_mgmt_objid?: string;
  request_date?: string;
  request_id?: string;
  remark?: string;
  writer?: string;
  lines: IssueRequestLineInput[];
}

export interface DispatchLineInput {
  objid: string;
  out_qty: number;
  out_date: string;
  acq_user: string;
  sign?: string;
}

export interface DispatchInput {
  master_objid: string;
  lines: DispatchLineInput[];
  writer?: string;
}

export const inventoryMngApi = {
  // 자재리스트
  async list(f: InventoryListFilter = {}): Promise<ListResponse> {
    const r = await apiClient.get("/inventory-mng/list", { params: f });
    return r.data?.data as ListResponse;
  },
  async saveStock(input: SaveStockInput): Promise<{ objid: string; in_objid: string }> {
    const r = await apiClient.post("/inventory-mng/stock", input);
    return r.data?.data;
  },
  async deleteStock(objids: string[]): Promise<{ deleted: number }> {
    const r = await apiClient.post("/inventory-mng/stock/delete", { objids });
    return r.data?.data;
  },
  async move(items: MoveItem[]): Promise<{ updated: number }> {
    const r = await apiClient.post("/inventory-mng/move", { items });
    return r.data?.data;
  },
  async history(objid: string): Promise<any[]> {
    const r = await apiClient.get(`/inventory-mng/history/${objid}`);
    return (r.data?.data ?? []) as any[];
  },

  // 불출의뢰
  async listIssue(f: IssueRequestFilter = {}): Promise<ListResponse> {
    const r = await apiClient.get("/inventory-mng/issue-request", { params: f });
    return r.data?.data as ListResponse;
  },
  async getIssue(objid: string): Promise<{ master: any; lines: any[] }> {
    const r = await apiClient.get(`/inventory-mng/issue-request/${objid}`);
    return r.data?.data;
  },
  async candidates(parent_objids: string[]): Promise<any[]> {
    const r = await apiClient.post("/inventory-mng/issue-request/candidates", { parent_objids });
    return (r.data?.data ?? []) as any[];
  },
  async saveIssue(input: IssueRequestSaveInput): Promise<{ master_objid: string; inventory_out_no: string }> {
    const r = await apiClient.post("/inventory-mng/issue-request", input);
    return r.data?.data;
  },
  async deleteIssue(objid: string): Promise<void> {
    await apiClient.delete(`/inventory-mng/issue-request/${objid}`);
  },
  async receiveIssue(objids: string[]): Promise<{ updated: number }> {
    const r = await apiClient.post("/inventory-mng/issue-request/receive", { objids });
    return r.data?.data;
  },
  async dispatchIssue(input: DispatchInput): Promise<{ updated: number }> {
    const r = await apiClient.post("/inventory-mng/issue-request/dispatch", input);
    return r.data?.data;
  },

  // 옵션
  async projects(): Promise<OptionItem[]> {
    const r = await apiClient.get("/inventory-mng/options/projects");
    return (r.data?.data ?? []) as OptionItem[];
  },
  async units(contract_objid: string): Promise<OptionItem[]> {
    const r = await apiClient.get("/inventory-mng/options/units", { params: { contract_objid } });
    return (r.data?.data ?? []) as OptionItem[];
  },
  async users(): Promise<OptionItem[]> {
    const r = await apiClient.get("/inventory-mng/options/users");
    return (r.data?.data ?? []) as OptionItem[];
  },
  async parts(keyword: string, limit = 30): Promise<OptionItem[]> {
    const r = await apiClient.get("/inventory-mng/options/parts", { params: { q: keyword, limit } });
    return (r.data?.data ?? []) as OptionItem[];
  },
};
