<template>
  <Transition name="address-slide">
    <div
      v-if="isOpen && addressUuid"
      class="fixed inset-x-0 bottom-0 z-50 flex flex-col"
      style="height: 70vh"
    >
      <!-- Backdrop (clickable to close) -->
      <div
        class="absolute inset-0 -top-[30vh] bg-black/30"
        @click="emit('close')"
      />

      <!-- Sheet content -->
      <div class="relative flex-1 bg-black/40 backdrop-blur-xl rounded-t-2xl border-t border-white/20 shadow-2xl overflow-hidden">
        <!-- Header with drag handle and close -->
        <div class="sticky top-0 z-10 bg-black/30 backdrop-blur-md border-b border-white/10">
          <div class="flex justify-center py-2">
            <div class="w-12 h-1.5 bg-white/30 rounded-full" />
          </div>
          <div class="flex items-center justify-between px-6 pb-4">
            <template v-if="address">
              <div class="flex items-center gap-3 flex-1 min-w-0">
                <div class="w-10 h-10 bg-emerald-500/20 rounded-xl flex items-center justify-center flex-shrink-0 text-2xl">
                  {{ isoToEmoji(address.countryCode) }}
                </div>
                <div class="min-w-0">
                  <div class="font-bold text-white truncate">{{ address.name }}</div>
                  <div class="text-sm text-white/60 truncate">{{ address.address }}</div>
                </div>
              </div>
            </template>

            <template v-else>
              <div class="flex items-center gap-3 flex-1">
                <div class="w-10 h-10 bg-white/10 rounded-xl animate-pulse" />
                <div class="flex-1">
                  <div class="h-5 bg-white/10 rounded w-48 animate-pulse" />
                  <div class="h-4 bg-white/10 rounded w-32 mt-1 animate-pulse" />
                </div>
              </div>
            </template>

            <button class="btn btn-ghost btn-sm btn-circle text-white/60 hover:text-white flex-shrink-0" @click="emit('close')">
              <Icon name="lucide:x" size="20" />
            </button>
          </div>
        </div>

        <!-- Content -->
        <div v-if="address" class="overflow-y-auto h-[calc(70vh-100px)] px-6 py-4 space-y-4">
          <!-- Location info -->
          <div class="bg-white/5 rounded-xl p-4 border border-white/10">
            <div class="font-semibold text-white mb-3 flex items-center gap-2">
              <Icon name="lucide:map-pin" size="18" />
              {{ t('profileAddresses.detail.location') }}
            </div>
            <div class="space-y-2 text-sm">
              <div class="flex items-start gap-2 text-white/80">
                <Icon name="lucide:navigation" size="14" class="text-white/50 mt-0.5 flex-shrink-0" />
                <span>{{ address.address }}</span>
              </div>
              <div v-if="address.latitude && address.longitude" class="flex items-center gap-2 text-white/60">
                <Icon name="lucide:crosshair" size="14" class="text-white/50" />
                <span class="font-mono text-xs">{{ address.latitude.toFixed(6) }}, {{ address.longitude.toFixed(6) }}</span>
              </div>
            </div>
          </div>

          <!-- Map preview -->
          <div v-if="address.latitude && address.longitude" class="bg-white/5 rounded-xl p-4 border border-white/10">
            <div class="font-semibold text-white mb-3 flex items-center gap-2">
              <Icon name="lucide:map" size="18" />
              {{ t('profileAddresses.detail.map') }}
            </div>
            <div class="h-48 rounded-lg overflow-hidden">
              <ClientOnly>
                <MapboxMap
                  :map-id="'address-preview-' + addressUuid"
                  style="width: 100%; height: 100%"
                  :options="{
                    style: 'mapbox://styles/mapbox/dark-v11',
                    center: [address.longitude, address.latitude],
                    zoom: 14,
                    interactive: false
                  }"
                >
                  <MapboxDefaultMarker
                    :marker-id="'address-marker'"
                    :lnglat="[address.longitude, address.latitude]"
                    color="#10b981"
                  />
                </MapboxMap>
              </ClientOnly>
            </div>
          </div>

          <!-- Actions -->
          <div class="flex gap-3">
            <NuxtLink :to="localePath(`/clientarea/addresses/${addressUuid}`)" class="flex-1">
              <button class="btn btn-sm w-full bg-white/10 border-white/20 text-white hover:bg-white/20">
                <Icon name="lucide:pencil" size="14" class="mr-2" />
                {{ t('profileAddresses.actions.edit') }}
              </button>
            </NuxtLink>
            <button
              class="btn btn-sm bg-error/20 border-error/30 text-error hover:bg-error/30"
              @click="handleDelete"
              :disabled="isDeleting"
            >
              <Icon name="lucide:trash-2" size="14" />
            </button>
          </div>
        </div>

        <!-- Loading state -->
        <div v-else class="px-6 py-4 space-y-4">
          <div class="h-24 bg-white/5 rounded-xl animate-pulse" />
          <div class="h-48 bg-white/5 rounded-xl animate-pulse" />
        </div>
      </div>
    </div>
  </Transition>
</template>

<script setup lang="ts">
const props = defineProps<{
  isOpen: boolean
  addressUuid: string | null
}>()

const emit = defineEmits<{
  'close': []
  'deleted': []
}>()

const { t } = useI18n()
const localePath = useLocalePath()

const { items, isoToEmoji, deleteAddress } = useTeamAddresses()

const isDeleting = ref(false)

const address = computed(() => {
  if (!props.addressUuid) return null
  return items.value.find(a => a.uuid === props.addressUuid) || null
})

const handleDelete = async () => {
  if (!props.addressUuid) return
  isDeleting.value = true
  const success = await deleteAddress(props.addressUuid)
  isDeleting.value = false
  if (success) {
    emit('deleted')
    emit('close')
  }
}
</script>

<style scoped>
.address-slide-enter-active,
.address-slide-leave-active {
  transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.3s ease;
}

.address-slide-enter-from,
.address-slide-leave-to {
  transform: translateY(100%);
  opacity: 0;
}

.address-slide-enter-to,
.address-slide-leave-from {
  transform: translateY(0);
  opacity: 1;
}
</style>
