import { apiClient } from "./client";

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

export interface OutboundItem {
  id: string;
  company_code: string;
  outbound_number: string;
  outbound_type: string;
  outbound_date: string;
  reference_number: string | null;
  customer_code: string | null;
  customer_name: string | null;
  item_code: string | null;
  item_name: string | null;
  specification: string | null;
  material: string | null;
  unit: string | null;
  outbound_qty: number;
  unit_price: number;
  total_amount: number;
  lot_number: string | null;
  warehouse_code: string | null;
  warehouse_name?: string | null;
  location_code: string | null;
  outbound_status: string;
  manager_id: string | null;
  memo: string | null;
  source_type: string | null;
  sales_order_id: string | null;
  shipment_plan_id: string | null;
  item_info_id: string | null;
  destination_code: string | null;
  delivery_destination: string | null;
  delivery_address: string | null;
  created_date: string;
  created_by: string | null;
}

export interface ShipmentInstructionSource {
  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;
  plan_qty: number;
  ship_qty: number;
  order_qty: number;
  remain_qty: number;
  source_type: 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;
  unit_price: number;
  status: string;
  due_date: 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 CreateOutboundPayload {
  outbound_number: string;
  outbound_date: string;
  warehouse_code?: string;
  location_code?: string;
  manager_id?: string;
  memo?: string;
  items: Array<{
    outbound_type: string;
    reference_number?: string;
    customer_code?: string;
    customer_name?: string;
    item_code?: string;
    item_number?: string;
    item_name?: string;
    spec?: string;
    specification?: string;
    material?: string;
    unit?: string;
    outbound_qty: number;
    unit_price?: number;
    total_amount?: number;
    lot_number?: string;
    warehouse_code?: string;
    location_code?: string;
    outbound_status?: string;
    manager_id?: string;
    memo?: string;
    source_type?: string;
    source_id?: string;
    sales_order_id?: string;
    shipment_plan_id?: string;
    item_info_id?: string;
    destination_code?: string;
    delivery_destination?: string;
    delivery_address?: string;
  }>;
}

// --- API 호출 ---

export async function getOutboundList(params?: {
  outbound_type?: string;
  outbound_status?: string;
  search_keyword?: string;
  date_from?: string;
  date_to?: string;
}) {
  const res = await apiClient.get("/outbound/list", { params });
  return res.data as { success: boolean; data: OutboundItem[] };
}

export async function createOutbound(payload: CreateOutboundPayload) {
  const res = await apiClient.post("/outbound", payload);
  return res.data as { success: boolean; data: OutboundItem[]; message?: string };
}

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

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

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

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

// 소스 데이터 조회
export async function getShipmentInstructionSources(keyword?: string) {
  const res = await apiClient.get("/outbound/source/shipment-instructions", {
    params: keyword ? { keyword } : {},
  });
  return res.data as { success: boolean; data: ShipmentInstructionSource[] };
}

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

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