<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useFetchJson } from '@/composable/useFetchJson'
import { i18n } from '@/plugins/02_i18n'
import Empty from '@/layouts/empty.vue'

const { t, te } = useI18n()
const router = useRouter()
const route = useRoute()

function tt(key: string, fallback: string): string {
  return te(key) ? t(key) : fallback
}

const token = ref('')
const loading = ref(false)
const error = ref<string | null>(null)
const success = ref(false)
const verificationInProgress = ref(true)

onMounted(() => {
  token.value = (route.query.token as string) || ''

  if (!token.value) {
    error.value = i18n.t('verify_email.invalid_token')
    verificationInProgress.value = false
    return
  }
  handleVerifyEmail()
})

async function handleVerifyEmail() {
  if (!token.value) {
    error.value = i18n.t('verify_email.invalid_token')
    return
  }

  loading.value = true
  error.value = null

  try {
    await useFetchJson('/api/v1/public/auth/complete-registration', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ token: token.value }),
    })

    success.value = true
    verificationInProgress.value = false

    setTimeout(() => {
      router.push({ name: 'login' })
    }, 3000)
  } catch (e: any) {
    error.value = e?.message ?? i18n.t('verify_email.verification_failed')
    verificationInProgress.value = false
  } finally {
    loading.value = false
  }
}

function goToLogin() {
  router.push({ name: 'login' })
}
</script>

<template>
  <component :is="Empty || 'div'">
    <div
      class="min-h-screen bg-gradient-to-br from-primary-50 via-white to-primary-100 dark:from-gray-900 dark:via-gray-800 dark:to-gray-900">
      <div class="pt-20 pb-8 flex items-center justify-center px-4 sm:px-6 lg:px-8">
        <div class="w-full max-w-md">
          <div class="text-center mb-8">
            <div
              class="inline-flex items-center justify-center w-16 h-16 rounded-2xl bg-primary-500 text-white mb-4 shadow-lg shadow-primary-500/30">
              <UIcon name="i-heroicons-clock" class="w-8 h-8" />
            </div>
            <h1 class="text-3xl font-bold text-gray-900 dark:text-white">B2B</h1>
          </div>

          <UCard class="shadow-xl shadow-gray-200/50 dark:shadow-gray-900/50">
            <template #header>
              <div class="text-center">
                <div v-if="verificationInProgress && loading">
                  <UIcon name="i-heroicons-arrow-path" class="w-8 h-8 animate-spin text-primary-500 mx-auto mb-4" />
                  <h2 class="text-xl font-semibold text-gray-900 dark:text-white">
                    {{ $t('verify_email.verifying') }}
                  </h2>
                </div>
                <div v-else-if="success">
                  <div
                    class="inline-flex items-center justify-center w-12 h-12 rounded-full bg-green-100 text-green-600 mb-4">
                    <UIcon name="i-heroicons-check-circle" class="w-6 h-6" />
                  </div>
                  <h2 class="text-xl font-semibold text-gray-900 dark:text-white">
                    {{ $t('verify_email.success_title') }}
                  </h2>
                  <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
                    {{ $t('verify_email.success_message') }}
                  </p>
                </div>
                <div v-else-if="error">
                  <div
                    class="inline-flex items-center justify-center w-12 h-12 rounded-full bg-red-100 text-red-600 mb-4">
                    <UIcon name="i-heroicons-exclamation-circle" class="w-6 h-6" />
                  </div>
                  <h2 class="text-xl font-semibold text-gray-900 dark:text-white">
                    {{ $t('verify_email.error_title') }}
                  </h2>
                  <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
                    {{ $t('verify_email.error_message') }}
                  </p>
                </div>
              </div>
            </template>

            <div v-if="success" class="text-center py-4">
              <p class="text-gray-600 dark:text-gray-400 mb-4">{{ $t('verify_email.redirect_message') }}</p>
              <UButton color="primary" @click="goToLogin">{{ $t('verify_email.go_to_login') }}</UButton>
            </div>
            <div v-else-if="error" class="text-center py-4">
              <UAlert :color="'error'" variant="subtle" icon="i-heroicons-exclamation-triangle" :title="error"
                class="mb-4" />
              <UButton color="primary" @click="goToLogin" class="cursor-pointer">{{ $t('verify_email.go_to_login') }}
              </UButton>
            </div>
            <div v-else-if="verificationInProgress && loading" class="text-center py-4">
              <p class="text-gray-500 dark:text-gray-400">{{ $t('verify_email.please_wait') }}</p>
            </div>

            <template #footer>
              <div class="text-center">
                <p class="text-sm text-gray-600 dark:text-gray-400">
                  {{ $t('verify_email.already_registered') }}
                  <button variant="link" size="sm" @click="goToLogin"
                    class="cursor-pointer text-(--text-sky-light) dark:text-(--text-sky-dark)"> {{
                      $t('general.sign_in')
                    }}
                  </button>
                </p>
              </div>
            </template>
          </UCard>
        </div>
      </div>
    </div>
  </component>
</template>
