<template>
  <div>
    <!-- Page Header -->
    <PageHeader 
      title="Member Dashboard"
      :subtitle="`Welcome back, ${memberData?.name || 'Member'}!`"
      theme="blue"
      size="medium"
    />

    <section class="py-12 bg-white dark:bg-gray-900">
      <UContainer>
        <div v-if="!memberData || authPending" class="flex justify-center items-center py-20">
          <div class="text-center">
            <div class="w-8 h-8 border-4 border-blue-500 border-t-transparent rounded-full animate-spin mx-auto mb-4"></div>
            <p class="text-gray-600 dark:text-gray-400">Loading your dashboard...</p>
          </div>
        </div>

        <div v-else class="space-y-8">
          <!-- Welcome Section -->
          <div class="bg-gradient-to-r from-blue-50 to-indigo-50 dark:from-blue-900/20 dark:to-indigo-900/20 rounded-2xl p-8">
            <div class="flex items-start justify-between">
              <div>
                <h1 class="text-2xl font-bold text-gray-900 dark:text-white mb-2">
                  Welcome to Ghost Guild, {{ memberData?.name }}!
                </h1>
                <p class="text-gray-600 dark:text-gray-300 mb-4">
                  Your membership is active and you're part of our cooperative community.
                </p>
                <div class="flex flex-wrap gap-4 text-sm">
                  <div class="bg-white dark:bg-gray-800 rounded-lg px-4 py-2 border">
                    <span class="text-gray-500">Circle:</span>
                    <span class="font-medium text-blue-600 dark:text-blue-400 ml-1 capitalize">{{ memberData?.circle }}</span>
                  </div>
                  <div class="bg-white dark:bg-gray-800 rounded-lg px-4 py-2 border">
                    <span class="text-gray-500">Contribution:</span>
                    <span class="font-medium text-green-600 dark:text-green-400 ml-1">${{ memberData?.contributionTier }} CAD/month</span>
                  </div>
                </div>
              </div>
              <div class="flex-shrink-0">
                <div class="w-16 h-16 bg-blue-500 rounded-full flex items-center justify-center text-white font-bold text-xl">
                  {{ memberData?.name?.charAt(0)?.toUpperCase() }}
                </div>
              </div>
            </div>
          </div>

          <!-- Quick Actions -->
          <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            <div class="bg-white dark:bg-gray-800 rounded-xl p-6 shadow-lg border border-gray-200 dark:border-gray-700">
              <div class="w-12 h-12 bg-blue-100 dark:bg-blue-900/30 rounded-lg flex items-center justify-center mb-4">
                <svg class="w-6 h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z" />
                </svg>
              </div>
              <h3 class="text-lg font-semibold mb-2 text-gray-900 dark:text-white">
                Upcoming Events
              </h3>
              <p class="text-gray-600 dark:text-gray-400 mb-4">
                Discover and register for community events and workshops.
              </p>
              <UButton to="/events" variant="outline" size="sm">
                View Events
              </UButton>
            </div>

            <div class="bg-white dark:bg-gray-800 rounded-xl p-6 shadow-lg border border-gray-200 dark:border-gray-700">
              <div class="w-12 h-12 bg-green-100 dark:bg-green-900/30 rounded-lg flex items-center justify-center mb-4">
                <svg class="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
                </svg>
              </div>
              <h3 class="text-lg font-semibold mb-2 text-gray-900 dark:text-white">
                Community
              </h3>
              <p class="text-gray-600 dark:text-gray-400 mb-4">
                Connect with other members in your circle and beyond.
              </p>
              <UButton to="/members" variant="outline" size="sm">
                Browse Members
              </UButton>
            </div>

            <div class="bg-white dark:bg-gray-800 rounded-xl p-6 shadow-lg border border-gray-200 dark:border-gray-700">
              <div class="w-12 h-12 bg-purple-100 dark:bg-purple-900/30 rounded-lg flex items-center justify-center mb-4">
                <svg class="w-6 h-6 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                </svg>
              </div>
              <h3 class="text-lg font-semibold mb-2 text-gray-900 dark:text-white">
                Account Settings
              </h3>
              <p class="text-gray-600 dark:text-gray-400 mb-4">
                Manage your profile and membership settings.
              </p>
              <UButton variant="outline" size="sm" disabled>
                Coming Soon
              </UButton>
            </div>
          </div>

          <!-- Recent Activity (Placeholder) -->
          <div class="bg-white dark:bg-gray-800 rounded-xl p-8 shadow-lg border border-gray-200 dark:border-gray-700">
            <h2 class="text-xl font-bold mb-6 text-gray-900 dark:text-white">
              Recent Activity
            </h2>
            <div class="text-center py-12">
              <svg class="w-16 h-16 text-gray-300 dark:text-gray-600 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4" />
              </svg>
              <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-2">
                No recent activity
              </h3>
              <p class="text-gray-500 dark:text-gray-400">
                Your activity and event history will appear here as you participate in the community.
              </p>
            </div>
          </div>
        </div>
      </UContainer>
    </section>
  </div>
</template>

<script setup>
const { memberData, checkMemberStatus } = useAuth()

// Handle authentication check on page load
const { pending: authPending } = await useLazyAsyncData('dashboard-auth', async () => {
  // Only check authentication on client side
  if (process.server) return null
  
  console.log('📊 Dashboard auth check - memberData exists:', !!memberData.value)
  
  // If no member data, try to authenticate
  if (!memberData.value) {
    console.log('  - No member data, checking authentication...')
    const isAuthenticated = await checkMemberStatus()
    console.log('  - Auth result:', isAuthenticated)
    
    if (!isAuthenticated) {
      console.log('  - Redirecting to login')
      await navigateTo('/login')
      return null
    }
  }
  
  console.log('  - ✅ Dashboard auth successful')
  return memberData.value
})

// Set page meta
useHead({
  title: 'Member Dashboard - Ghost Guild'
})

// Removed middleware - handling auth directly in the page component
</script>