<template>
  <Teleport to="body">
    <div
      v-if="!isServerAvailable"
      class="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm flex items-center justify-center"
      @click.prevent
      @keydown.prevent
    >
      <div class="bg-white dark:bg-gray-900 rounded-lg shadow-xl p-8 max-w-md mx-4 text-center">
        <!-- Loading Spinner -->
        <div class="mb-6 flex justify-center">
          <UIcon name="i-heroicons-arrow-path" class="w-12 h-12 text-primary-500 animate-spin" />
        </div>

        <!-- Title -->
        <h2 class="text-xl font-semibold text-gray-900 dark:text-white mb-4">
          {{ t('serverConnection.title') }}
        </h2>

        <!-- Description -->
        <p class="text-gray-600 dark:text-gray-400 mb-6 leading-relaxed">
          {{ t('serverConnection.message') }}
        </p>

        <!-- Status Information -->
        <div class="space-y-2 text-sm text-gray-500 dark:text-gray-400">
          <div v-if="isChecking" class="flex items-center justify-center gap-2">
            <UIcon name="i-heroicons-arrow-path" class="w-4 h-4 animate-spin" />
            <span>{{ t('serverConnection.checking') }}</span>
          </div>

          <div v-if="lastCheckTime" class="text-xs">
            {{ t('serverConnection.lastCheck') }}:
            {{ new Date(lastCheckTime).toLocaleTimeString() }}
          </div>

          <div class="text-xs">
            {{ t('serverConnection.retryInfo') }}
          </div>
        </div>

        <!-- Optional: Manual retry button -->
        <UButton v-if="!isChecking" variant="ghost" size="sm" class="mt-4" @click="void checkServerHealth()">
          <UIcon name="i-heroicons-arrow-path" class="w-4 h-4 mr-1" />
          {{ t('serverConnection.retryNow') }}
        </UButton>
      </div>
    </div>
  </Teleport>
</template>

<script setup lang="ts">
const { t } = useI18n()
const { isServerAvailable, isChecking, lastCheckTime, checkServerHealth } = useServerHealth()
</script>
