import type {
  KbxWmsExceptionCommand,
  KbxWmsPickingTask,
  KbxWmsScanCommand,
  KbxWmsScanResult,
  KbxWmsSetQuantityCommand,
} from '@kbx/contracts'
import { kbxApi } from '../../../http/generated/kbxApiClient'

export function getPickingTask(taskId: string) {
  return kbxApi.request<KbxWmsPickingTask>('wms.picking.getTask', { path: { taskId } })
}

export function startPickingTask(taskId: string, expectedVersion: number, idempotencyKey = crypto.randomUUID()) {
  return kbxApi.request<KbxWmsPickingTask, { expectedVersion: number }>('wms.picking.start', {
    path: { taskId }, body: { expectedVersion }, idempotencyKey,
  })
}

export function scanPickingBarcode(command: KbxWmsScanCommand) {
  return kbxApi.request<KbxWmsScanResult, KbxWmsScanCommand>('wms.picking.scan', {
    path: { taskId: command.taskId }, body: command, idempotencyKey: command.idempotencyKey,
  })
}

export function reportPickingException(command: KbxWmsExceptionCommand) {
  return kbxApi.request<KbxWmsPickingTask, KbxWmsExceptionCommand>('wms.picking.reportException', {
    path: { taskId: command.taskId }, body: command, idempotencyKey: command.idempotencyKey,
  })
}

export function setPickingQuantity(command: KbxWmsSetQuantityCommand) {
  return kbxApi.request<KbxWmsScanResult, KbxWmsSetQuantityCommand>('wms.picking.setQuantity', {
    path: { taskId: command.taskId }, body: command, idempotencyKey: command.idempotencyKey,
  })
}
