import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { FbaAppointmentDetailsPayload, FbaAppointmentDto } from '@/modules/overseas-warehouse' export interface ReceivingSource { sourceKey: string; destinationKey: string; packageCount: number; sourceVersion: string; verificationVersion: number | null; inboundCode: string | null } export interface ReceivingMember { batchKey: string; appointmentNo: string; batchVersion: number; customerId: string; sources: ReceivingSource[]; customerName: string | null } export interface ReceivingAgreement { agreementKey: string; version: number; status: string; destinationKey: string; details: FbaAppointmentDetailsPayload; timezone: string | null; members: ReceivingMember[]; warehouseEvidence: string } export interface ReceivingTransport { version: string; vehicles: Array<{ tripNo: string | null; version: number | null; dispatchNo: string | null; customerId: string; batchKey: string; status: string; packageCount: number }>; blockReasons: string[] } export interface ReceivingQuantity { sourceKey: string; destinationKey: string; retainedPackages: number; expectedSourceVersion: string; expectedVerificationVersion: number | null } export interface ReceivingIntent { batchKey: string; batchVersion: number; customerId: string; quantities: ReceivingQuantity[]; laterDate: string | null; ownerId: string; nextFollowupDate: string | null; reason: string; transportVersion: string } export interface ReceivingExecutionScope { version: string; members: Array<{ member: { batchKey: string; customerId: string }; completed: boolean; canRebook: boolean; reason: string | null }> } export interface ReceivingPending { changeKey: string; version: number; status: string; intent: ReceivingIntent; originalMembers: ReceivingMember[]; originalAgreements: ReceivingAgreement[]; followup: { ownerId: string; ownerName: string | null; nextFollowupDate: string | null } } export interface ReceivingCustomerPlan { batchKey: string; customerId: string; details: FbaAppointmentDetailsPayload; timezone: string | null } export interface ReceivingCargoRefresh { idempotencyKey: string; member: Pick; sources: Array<{ sourceKey: string; destinationKey: string; expectedSourceVersion: string; expectedVerificationVersion: number | null }>; executionVersion: string } export interface ReceivingPlan { idempotencyKey: string; member: Pick; details: FbaAppointmentDetailsPayload; executionVersion: string } export interface ReceivingContext { batch: FbaAppointmentDto; plans: ReceivingCustomerPlan[]; agreements: ReceivingAgreement[]; pendingChanges: ReceivingPending[]; transport: ReceivingTransport; executionScope: ReceivingExecutionScope; allowedActions: string[]; blockReasons: string[] } export interface ReceivingGroup { agreement: ReceivingAgreement; eligibleMembers: ReceivingMember[]; excludedMembers: Array<{ member: ReceivingMember; reason: string; currentAgreementKey: string | null }>; transport: ReceivingTransport; executionScope: ReceivingExecutionScope } export interface ReceivingResult { changeKey: string | null; status: string; batches: FbaAppointmentDto[]; agreements: ReceivingAgreement[]; steps: Array<{ step: string; status: string; targetKey: string; message: string }>; nextActions: string[] } export interface ReceivingPropose { idempotencyKey: string; intent: ReceivingIntent } export interface ReceivingResolve { idempotencyKey: string; version: number; decision: 'CONFIRM' | 'WITHDRAW'; warehouseEvidence: string | null; transportVersion: string } export interface ReceivingBooking { members: Array>; details: FbaAppointmentDetailsPayload; warehouseEvidence: string } export interface ReceivingBook { idempotencyKey: string; bookings: ReceivingBooking[]; cancelledAgreementKey: string | null; cancelledAgreementVersion: number | null; executionVersion: string } export interface ReceivingGroupChange { idempotencyKey: string; version: number; action: 'RESCHEDULE' | 'CANCEL'; details: FbaAppointmentDetailsPayload | null; warehouseEvidence: string; transportVersion: string } const base = '/platform/v1/last-leg/appointment-receiving' export async function getReceivingContext(batchKey: string) { return unwrapApiResponse(await http.get, ApiEnvelope>(`${base}/batches/${encodeURIComponent(batchKey)}`)) } export async function getReceivingGroup(agreementKey: string) { return unwrapApiResponse(await http.get, ApiEnvelope>(`${base}/agreements/${encodeURIComponent(agreementKey)}`)) } async function post(path: string, request: ReceivingCargoRefresh | ReceivingPlan | ReceivingPropose | ReceivingResolve | ReceivingBook | ReceivingGroupChange) { return unwrapApiResponse(await http.post, ApiEnvelope>(`${base}${path}`, request)) } export const proposeReceivingChange = (request: ReceivingPropose) => post('/changes', request) export const resolveReceivingChange = (changeKey: string, request: ReceivingResolve) => post(`/changes/${encodeURIComponent(changeKey)}/resolve`, request) export const bookReceiving = (request: ReceivingBook) => post('/bookings', request) export const changeReceivingGroup = (agreementKey: string, request: ReceivingGroupChange) => post(`/agreements/${encodeURIComponent(agreementKey)}/${request.action === 'CANCEL' ? 'cancel' : 'reschedule'}`, request) export const saveReceivingPlan = (request: ReceivingPlan) => post('/plans', request) export async function getReceivingExecutionScope(members: Array<{ batchKey: string; customerId: string }>) { return unwrapApiResponse(await http.post, ApiEnvelope>(`${base}/execution-scope`, { members })) } export const refreshReceivingCargo = (request: ReceivingCargoRefresh) => post('/cargo-refresh', request)