import { apiClient } from "./client";

// ===== 타입 =====

export interface OutsourcingCandidate {
  completed_process_id: string;
  wo_id: string;
  completed_seq_no: string;
  completed_process_code: string;
  completed_process_name: string;
  good_qty: number;
  next_process_id: string;
  next_seq_no: string;
  next_process_code: string;
  next_process_name: string;
  next_status: string;
  instruction_no: string;
  item_code: string;
  item_name: string;
  spec: string;
  material: string;
  unit: string;
  subcontractor_id: string;
  subcontractor_code: string;
  subcontractor_name: string;
}

export interface OutsourcingOutboundItem {
  id: string;
  outbound_number: string;
  outbound_type: string;
  outbound_date: string;
  reference_number: string;
  customer_code: string;
  customer_name: string;
  item_code: string;
  item_name: string;
  specification: string;
  material: string;
  unit: string;
  outbound_qty: number;
  warehouse_code: string;
  warehouse_name?: string;
  location_code: string;
  outbound_status: string;
  source_type: string;
  source_id: string;
  manager_id: string;
  memo: string;
}

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

// ===== API 함수 =====

export async function getCandidates(keyword?: string) {
  const params: Record<string, string> = {};
  if (keyword) params.keyword = keyword;
  const res = await apiClient.get("/outsourcing-outbound/candidates", { params });
  return res.data as { success: boolean; data: OutsourcingCandidate[] };
}

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

export async function createOutsourcingOutbound(payload: {
  outbound_number: string;
  outbound_date: string;
  warehouse_code?: string;
  location_code?: string;
  manager_id?: string;
  memo?: string;
  items: Array<{
    reference_number?: string;
    subcontractor_code?: string;
    subcontractor_name?: string;
    item_code?: string;
    item_name?: string;
    spec?: string;
    material?: string;
    unit?: string;
    outbound_qty: number;
    completed_process_id?: string;
    warehouse_code?: string;
    location_code?: string;
  }>;
}) {
  const res = await apiClient.post("/outsourcing-outbound", payload);
  return res.data as { success: boolean; data: OutsourcingOutboundItem[]; message?: string };
}

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

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

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

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