import http from './http'
import type { DeliveryTask } from '@/types/delivery'

const MOCK = import.meta.env.VITE_USE_MOCK === 'true'

const BASE = '/sl/projection/deliveries'

export async function listDeliveries(opts?: {
  includeFinished?: boolean
  includeAborted?: boolean
}): Promise<DeliveryTask[]> {
  if (MOCK) {
    const { mockDeliveries } = await import('@/mock/data/deliveries')
    return mockDeliveries()
  }
  const params: Record<string, string> = {}
  if (opts?.includeFinished === false) params.includeFinished = 'false'
  if (opts?.includeAborted === false) params.includeAborted = 'false'
  const { data } = await http.get<DeliveryTask[]>(BASE, { params })
  return Array.isArray(data) ? data : []
}

export async function cancelDelivery(id: number): Promise<void> {
  if (MOCK) return
  await http.post(`${BASE}/${id}/cancel`)
}

export async function resendDelivery(id: number): Promise<void> {
  if (MOCK) return
  await http.post(`${BASE}/${id}/resend`)
}

export async function forceCompleteDelivery(id: number): Promise<void> {
  if (MOCK) return
  await http.post(`${BASE}/${id}/force-complete`)
}
