import { overseasHttp } from '@/shared/api/overseasHttp' import { unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import { normalizeOverseasPage, type OverseasPagePayload } from './lastMileOrders' import type { DeliveryOrderRow } from './deliveryOrders' export interface OutboundRow { id: string; outboundNo: string; dispatchNo?: string | null; overseasWarehouseId?: string | null; lxCodes?: string | null; status?: string | null; palletCount?: number | null; packageCount?: number | null; grossWeightKg?: number | null; volumeWeightKg?: number | null; volumeCbm?: number | null; createdAt?: string | null; outboundType?: string | null } export interface OutboundPalletRow { id?: string | null palletNo?: string | null cartonMarks?: string | null inboundCode?: string | null destinationWarehouseCode?: string | null destinationWarehouseName?: string | null skuCode?: string | null packageCount?: number | null grossWeightKg?: number | null volumeWeightKg?: number | null volumeCbm?: number | null pickedPackageCount?: number | null status?: string | null } export interface OutboundDetail { outbound: OutboundRow; pallets?: OutboundPalletRow[]; deliveries?: DeliveryOrderRow[] } export interface OutboundQuery { page: number; size: number; documentNo?: string; lxCode?: string; warehouseId?: string; status?: string; outboundType?: string } export interface ReturnOutboundCandidate { returnNo?: string | null; overseasWarehouseId?: string | null; cartonMark?: string | null; palletCode?: string | null; fbaCartonMark?: string | null; destinationWarehouseCode?: string | null; destinationWarehouseName?: string | null; totalQuantity?: number | null; status?: string | null } export interface OneClickOutboundCandidate { inventoryPositionId: string; orderNo?: string | null; customerId?: string | null; customerName?: string | null; overseasWarehouseId?: string | null; overseasWarehouseName?: string | null; palletNo?: string | null; inboundCode?: string | null; skuCode?: string | null; productName?: string | null; availablePackages: number; grossWeightKg: number; volumeWeightKg: number; volumeCbm: number } export interface WarehouseTransferOutboundCandidate { customerId: string; customerName?: string | null; transferNo: string; overseasWarehouseId: string; transferStatus: string; needRelabel: boolean; relabelNo?: string | null; destinationType?: 'FBA' | 'PRIVATE' | null; destinationWarehouseCode?: string | null; privateDestinationAddress?: string | null; privateDestinationPostalCode?: string | null; packageCount: number; grossWeightKg?: number | null; volumeCbm?: number | null } export interface WarehouseTransferDestinationPayload { transfers: Array<{ customerId: string; transferNo: string }>; destinationType: 'FBA' | 'PRIVATE'; destinationWarehouseId?: string; destinationWarehouseCode?: string; destinationWarehouseName?: string; privateDestinationAddress?: string; privateDestinationPostalCode?: string; privateDestinationContactName?: string; privateDestinationContactPhone?: string; idempotencyKey: string } export async function listOutbounds(query: OutboundQuery) { const response = await overseasHttp.get>, ApiEnvelope>>('/platform/v1/last-leg/outbound-orders', { params: query }) return normalizeOverseasPage(unwrapApiResponse(response)) } export async function getOutbound(outboundNo: string) { const response = await overseasHttp.get, ApiEnvelope>(`/platform/v1/last-leg/outbound-orders/${encodeURIComponent(outboundNo)}`) return unwrapApiResponse(response) } export async function createOneClickDropshipOutbound(customerId: string, payload: { recipient: string; items: Array<{ inventoryPositionId: string; packages: number; sku?: string; cartonMarks?: string[] }>; remark?: string; idempotencyKey: string }) { const response = await overseasHttp.post, ApiEnvelope>('/platform/v1/last-leg/outbound-orders/one-click-dropship', payload, { params: { customerId } }) return unwrapApiResponse(response) } export async function listOneClickOutboundCandidates(customerId?: string, query: { warehouseId?: string } = {}) { const response = await overseasHttp.get, ApiEnvelope>('/platform/v1/last-leg/outbound-orders/one-click-dropship/candidates', { params: { customerId, ...query } }) return unwrapApiResponse(response).map((item) => ({ ...item, inventoryPositionId: String(item.inventoryPositionId), availablePackages: Number(item.availablePackages || 0), grossWeightKg: Number(item.grossWeightKg || 0), volumeWeightKg: Number(item.volumeWeightKg || 0), volumeCbm: Number(item.volumeCbm || 0), })) } export async function listReturnOutboundCandidates(customerId: string, warehouseId: string) { const response = await overseasHttp.get, ApiEnvelope>('/platform/v1/last-leg/outbound-orders/return/candidates', { params: { customerId, warehouseId } }) return unwrapApiResponse(response) } export async function createReturnOutbound(customerId: string, payload: { recipient: string; overseasWarehouseId: string; returnNo: string; cartonMarks: string[]; skuCode?: string; unitType?: 'BOX' | 'PALLET'; remark?: string; idempotencyKey: string }) { const response = await overseasHttp.post, ApiEnvelope>('/platform/v1/last-leg/outbound-orders/return', payload, { params: { customerId } }) return unwrapApiResponse(response) } export async function listWarehouseTransferOutboundCandidates(query: { customerId?: string; warehouseId?: string } = {}) { const response = await overseasHttp.get, ApiEnvelope>('/platform/v1/last-leg/outbound-orders/warehouse-transfer/candidates', { params: query }) return unwrapApiResponse(response).map(item => ({ ...item, packageCount: Number(item.packageCount || 0) })) } export async function bindWarehouseTransferDestination(payload: WarehouseTransferDestinationPayload) { const response = await overseasHttp.post, ApiEnvelope>('/platform/v1/last-leg/warehouse-transfers/bind-destination', payload) return unwrapApiResponse(response) } export async function cancelOutbound(outboundNo: string, reason: string) { const response = await overseasHttp.post, ApiEnvelope>(`/platform/v1/last-leg/outbound-orders/${encodeURIComponent(outboundNo)}/cancel`, { reason }) return unwrapApiResponse(response) }