import type { GetTeamOrdersQueryResult } from '~/composables/graphql/team/orders-generated'

type TeamOrder = NonNullable<NonNullable<GetTeamOrdersQueryResult['getTeamOrders']>[number]>
type TeamOrderStage = NonNullable<NonNullable<TeamOrder['stages']>[number]>

const items = ref<TeamOrder[]>([])
const isLoading = ref(false)
const isInitialized = ref(false)

export function useTeamOrders() {
  const { t } = useI18n()
  const { execute } = useGraphQL()

  const filters = computed(() => [
    { id: 'all', label: t('ordersList.filters.all') },
    { id: 'pending', label: t('ordersList.filters.pending') },
    { id: 'processing', label: t('ordersList.filters.processing') },
    { id: 'in_transit', label: t('ordersList.filters.in_transit') },
    { id: 'delivered', label: t('ordersList.filters.delivered') }
  ])

  const selectedFilter = ref('all')

  const filteredItems = computed(() => {
    if (selectedFilter.value === 'all') return items.value
    return items.value.filter(order => order.status === selectedFilter.value)
  })

  const routesForMap = computed(() =>
    filteredItems.value
      .filter(order => order.uuid && order.name)
      .map(order => ({
        uuid: order.uuid!,
        name: order.name!,
        status: order.status ?? undefined,
        stages: (order.stages || [])
          .filter((s): s is TeamOrderStage => s !== null && s.stageType === 'transport' && !!s.sourceLatitude && !!s.sourceLongitude && !!s.destinationLatitude && !!s.destinationLongitude)
          .map((s) => ({
            fromLat: s.sourceLatitude,
            fromLon: s.sourceLongitude,
            toLat: s.destinationLatitude,
            toLon: s.destinationLongitude,
            fromName: s.sourceLocationName,
            toName: s.destinationLocationName,
            transportType: s.transportType
          }))
      }))
      .filter(order => order.stages.length > 0)
  )

  const load = async () => {
    isLoading.value = true
    try {
      const { GetTeamOrdersDocument } = await import('~/composables/graphql/team/orders-generated')
      const data = await execute(GetTeamOrdersDocument, {}, 'team', 'orders')
      items.value = (data?.getTeamOrders || []).filter((o): o is TeamOrder => o !== null)
      isInitialized.value = true
    } catch (e) {
      console.error('Failed to load orders', e)
    } finally {
      isLoading.value = false
    }
  }

  const init = async () => {
    if (!isInitialized.value && items.value.length === 0) {
      await load()
    }
  }

  const getStatusVariant = (status: string) => {
    const variants: Record<string, string> = {
      pending: 'warning',
      processing: 'primary',
      in_transit: 'info',
      delivered: 'success',
      cancelled: 'error'
    }
    return variants[status] || 'muted'
  }

  const getStatusText = (status: string) => {
    const texts: Record<string, string> = {
      pending: t('ordersDetail.status.pending'),
      processing: t('ordersDetail.status.processing'),
      in_transit: t('ordersDetail.status.in_transit'),
      delivered: t('ordersDetail.status.delivered'),
      cancelled: t('ordersDetail.status.cancelled')
    }
    return texts[status] || status || t('ordersDetail.status.unknown')
  }

  return {
    items,
    filteredItems,
    isLoading,
    filters,
    selectedFilter,
    routesForMap,
    load,
    init,
    getStatusVariant,
    getStatusText
  }
}
