<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import Navbar from '@/components/layout/Navbar.vue'
import Footer from '@/components/layout/Footer.vue'
import ConnectionStatus from '@/components/nostr/ConnectionStatus.vue'
import PasswordDialog from '@/components/nostr/PasswordDialog.vue'
import { Toaster } from '@/components/ui/sonner'
import 'vue-sonner/style.css'
import { useNostr } from '@/composables/useNostr'
import { identity } from '@/composables/useIdentity'
import { toast } from 'vue-sonner'
import { useNostrStore } from '@/stores/nostr'

const { isConnected, isConnecting, error, connect, disconnect } = useNostr()
const nostrStore = useNostrStore()

const showPasswordDialog = ref(false)

async function handlePasswordUnlock(password: string) {
  try {
    await identity.loadWithPassword(password)
    showPasswordDialog.value = false
    toast.success('Identity unlocked successfully')
  } catch (error) {
    toast.error('Failed to unlock identity. Please check your password.')
  }
}

function handlePasswordCancel() {
  showPasswordDialog.value = false
}

onMounted(async () => {
  // Check if we have an encrypted identity that needs password
  if (identity.hasStoredIdentity() && identity.isStoredIdentityEncrypted()) {
    showPasswordDialog.value = true
  } else {
    // Initialize identity system for non-encrypted identities
    try {
      await identity.initialize()
    } catch (error) {
      console.error('Failed to initialize identity:', error)
    }
  }

  // Connect to Nostr relays
  await connect()

  // Check push notification status
  await nostrStore.checkPushNotificationStatus()
})

onUnmounted(() => {
  disconnect()
})
</script>

<template>
  <div class="min-h-screen bg-background font-sans antialiased">
    <div class="relative flex min-h-screen flex-col"
      style="padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom)">
      <header
        class="sticky top-0 z-50 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
        <nav
          class="w-full max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 xl:px-12 2xl:px-16 flex h-14 lg:h-16 xl:h-20 items-center justify-between">
          <Navbar />
        </nav>
      </header>

      <main class="flex-1">
        <router-view />
      </main>

      <Footer />
    </div>

    <!-- Toast notifications -->
    <Toaster />

    <!-- Password unlock dialog -->
    <PasswordDialog v-model:is-open="showPasswordDialog" title="Unlock Identity"
      description="Your Nostr identity is encrypted. Enter your password to unlock it." @password="handlePasswordUnlock"
      @cancel="handlePasswordCancel" />
  </div>
</template>
