import { apiClient } from "./client";

// --- 타입 정의 ---

export interface InboundItem {
  id: string;
  company_code: string;
  inbound_number: string;
  inbound_type: string;
  inbound_date: string;
  reference_number: string | null;
  supplier_code: string | null;
  supplier_name: string | null;
  item_number: string | null;
  item_name: string | null;
  spec: string | null;
  material: string | null;
  unit: string | null;
  inbound_qty: number;
  unit_price: number;
  total_amount: number;
  lot_number: string | null;
  warehouse_code: string | null;
  warehouse_name?: string | null;
  location_code: string | null;
  inbound_status: string;
  inspection_status: string | null;
  inspector: string | null;
  manager: string | null;
  memo: string | null;
  source_table: string | null;
  source_id: string | null;
  created_date: string;
  created_by: string | null;
}

export interface PurchaseOrderSource {
  id: string;
  purchase_no: string;
  order_date: string;
  supplier_code: string;
  supplier_name: string;
  item_code: string;
  item_name: string;
  spec: string | null;
  material: string | null;
  order_qty: number;
  received_qty: number;
  remain_qty: number;
  unit_price: number;
  status: string;
  due_date: string | null;
}

export interface ShipmentSource {
  detail_id: number;
  instruction_id: number;
  instruction_no: string;
  instruction_date: string;
  partner_id: string;
  instruction_status: string;
  item_code: string;
  item_name: string;
  spec: string | null;
  material: string | null;
  ship_qty: number;
  order_qty: number;
  source_type: string | null;
}

export interface ItemSource {
  id: string;
  item_number: string;
  item_name: string;
  spec: string | null;
  material: string | null;
  unit: string | null;
  standard_price: number;
}

export interface WarehouseOption {
  warehouse_code: string;
  warehouse_name: string;
  warehouse_type: string;
}

export interface CreateReceivingPayload {
  inbound_number: string;
  inbound_date: string;
  warehouse_code?: string;
  location_code?: string;
  inspector?: string;
  manager?: string;
  memo?: string;
  items: Array<{
    inbound_type: string;
    reference_number?: string;
    supplier_code?: string;
    supplier_name?: string;
    item_number?: string;
    item_name?: string;
    spec?: string;
    material?: string;
    unit?: string;
    inbound_qty: number;
    unit_price?: number;
    total_amount?: number;
    lot_number?: string;
    warehouse_code?: string;
    location_code?: string;
    inbound_status?: string;
    inspection_status?: string;
    inspector?: string;
    manager?: string;
    memo?: string;
    source_table: string;
    source_id: string;
  }>;
}

// --- API 호출 ---

export async function getReceivingList(params?: {
  inbound_type?: string;
  inbound_status?: string;
  search_keyword?: string;
  date_from?: string;
  date_to?: string;
}) {
  const res = await apiClient.get("/receiving/list", { params });
  return res.data as { success: boolean; data: InboundItem[] };
}

export async function createReceiving(payload: CreateReceivingPayload) {
  const res = await apiClient.post("/receiving", payload);
  return res.data as { success: boolean; data: InboundItem[]; message?: string };
}

export async function updateReceiving(id: string, payload: Partial<InboundItem>) {
  const res = await apiClient.put(`/receiving/${id}`, payload);
  return res.data as { success: boolean; data: InboundItem };
}

export async function deleteReceiving(id: string) {
  const res = await apiClient.delete(`/receiving/${id}`);
  return res.data as { success: boolean; message?: string };
}

export async function generateReceivingNumber() {
  const res = await apiClient.get("/receiving/generate-number");
  return res.data as { success: boolean; data: string };
}

export async function getReceivingWarehouses() {
  const res = await apiClient.get("/receiving/warehouses");
  return res.data as { success: boolean; data: WarehouseOption[] };
}

// 소스 데이터 조회
export async function getPurchaseOrderSources(keyword?: string) {
  const res = await apiClient.get("/receiving/source/purchase-orders", {
    params: keyword ? { keyword } : {},
  });
  return res.data as { success: boolean; data: PurchaseOrderSource[] };
}

export async function getShipmentSources(keyword?: string) {
  const res = await apiClient.get("/receiving/source/shipments", {
    params: keyword ? { keyword } : {},
  });
  return res.data as { success: boolean; data: ShipmentSource[] };
}

export async function getItemSources(keyword?: string) {
  const res = await apiClient.get("/receiving/source/items", {
    params: keyword ? { keyword } : {},
  });
  return res.data as { success: boolean; data: ItemSource[] };
}
