<template>
  <CatalogPage
    :items="itemsWithCoords"
    :loading="isLoading"
    map-id="select-location-map"
    point-color="#10b981"
    :selected-id="selectedHubId"
    v-model:hovered-id="hoveredHubId"
    :has-sub-nav="false"
    @select="selectHub"
  >
    <template #header>
      <Stack gap="4">
        <!-- Back button -->
        <div class="flex justify-between items-center">
          <Heading :level="2">{{ t('common.selectLocation') }}</Heading>
          <button class="btn btn-ghost btn-sm" @click="router.back()">
            <Icon name="lucide:x" size="20" />
          </button>
        </div>

        <!-- My addresses section -->
        <Stack v-if="isAuthenticated && teamAddresses?.length" gap="3">
          <Text weight="semibold">{{ t('profileAddresses.header.title') }}</Text>
          <Stack gap="2">
            <Card
              v-for="addr in teamAddresses"
              :key="addr.uuid"
              padding="sm"
              interactive
              :class="{ 'ring-2 ring-primary': isSelected('address', addr.uuid) }"
              @click="selectAddress(addr)"
            >
              <Stack gap="1">
                <Stack direction="row" align="center" gap="2">
                  <Icon name="lucide:map-pin" size="16" class="text-primary" />
                  <Text size="sm" weight="semibold">{{ addr.name }}</Text>
                  <Pill v-if="addr.isDefault" variant="outline" size="sm">Default</Pill>
                </Stack>
                <Text tone="muted" size="xs">{{ addr.address }}</Text>
              </Stack>
            </Card>
          </Stack>
        </Stack>

        <!-- Hubs section header -->
        <Text weight="semibold">{{ t('catalogMap.hubsTab') }}</Text>
      </Stack>
    </template>

    <template #filters>
      <CatalogFilterSelect :filters="filters" v-model="selectedFilter" />
    </template>

    <template #card="{ item }">
      <HubCard
        :hub="item"
        selectable
        :is-selected="isSelected('hub', item.uuid)"
      />
    </template>

    <template #pagination>
      <PaginationLoadMore
        :shown="items.length"
        :total="total"
        :can-load-more="canLoadMore"
        :loading="isLoadingMore"
        @load-more="loadMore"
      />
    </template>

    <template #empty>
      <EmptyState :title="t('catalogMap.noHubs')" />
    </template>
  </CatalogPage>
</template>

<script setup lang="ts">
import { useLocationStore } from '~/stores/location'
import type { CatalogHubItem, CatalogNearestHubItem } from '~/composables/useCatalogHubs'
import type { TeamAddress } from '~/composables/graphql/team/teams-generated'

definePageMeta({
  layout: 'topnav'
})

const router = useRouter()
const route = useRoute()
const { t } = useI18n()
const localePath = useLocalePath()
const { isAuthenticated } = useAuth()
const locationStore = useLocationStore()
const searchStore = useSearchStore()
const isSearchMode = computed(() => route.query.mode === 'search')

// Use shared composable for hubs
const {
  items,
  total,
  selectedFilter,
  filters,
  isLoading,
  isLoadingMore,
  itemsWithCoords,
  canLoadMore,
  loadMore,
  init
} = useCatalogHubs()

// Selected/hovered hub for map
const selectedHubId = ref<string | undefined>()
const hoveredHubId = ref<string | undefined>()

await init()

// Load team addresses
const teamAddresses = ref<TeamAddress[]>([])

if (isAuthenticated.value) {
  try {
    const { execute } = useGraphQL()
    const { GetTeamAddressesDocument } = await import('~/composables/graphql/team/teams-generated')
    const data = await execute(GetTeamAddressesDocument, {}, 'team', 'teams')
    teamAddresses.value = (data?.teamAddresses || []).filter((a): a is TeamAddress => a != null)
  } catch {
    // Not critical
  }
}

const isSelected = (type: 'address' | 'hub', uuid: string) => {
  return locationStore.selectedLocation?.type === type && locationStore.selectedLocation?.uuid === uuid
}

const goToRequestIfReady = () => {
  if (route.query.after === 'request' && searchStore.searchForm.productUuid && searchStore.searchForm.locationUuid) {
    const query: Record<string, string> = {
      product: searchStore.searchForm.productUuid,
      hub: searchStore.searchForm.locationUuid
    }
    if (searchStore.searchForm.quantity) query.quantity = String(searchStore.searchForm.quantity)

    router.push({
      path: `/catalog`,
      query
    })
    return true
  }
  return false
}

const selectHub = async (hub: CatalogHubItem | CatalogNearestHubItem) => {
  if (!hub.uuid) return
  selectedHubId.value = hub.uuid

  if (isSearchMode.value) {
    searchStore.setLocation(hub.name ?? '')
    searchStore.setLocationUuid(hub.uuid)
    if (goToRequestIfReady()) return
    router.back()
    return
  }

  try {
    const success = await locationStore.select('hub', hub.uuid, hub.name ?? '', hub.latitude ?? 0, hub.longitude ?? 0)
    if (success) {
      router.back()
    }
  } catch (e) {
    console.error('[selectHub] Error:', e)
  }
}

const selectAddress = async (addr: TeamAddress) => {
  if (isSearchMode.value) {
    searchStore.setLocation(addr.address || addr.name)
    searchStore.setLocationUuid(addr.uuid)
    if (goToRequestIfReady()) return
    router.back()
    return
  }

  try {
    const success = await locationStore.select('address', addr.uuid, addr.name, addr.latitude ?? 0, addr.longitude ?? 0)
    if (success) {
      router.back()
    }
  } catch (e) {
    console.error('[selectAddress] Error:', e)
  }
}
</script>
