<script setup lang="ts">
import { onMounted } from 'vue'
import { useAdmin } from './composables/useAdmin'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Skeleton } from '@/components/ui/skeleton'
import { Alert, AlertDescription } from '@/components/ui/alert'

const { stats, isLoading, error, fetchStats } = useAdmin()

onMounted(() => {
  fetchStats()
})

function formatCurrency(cents: number, currency = 'GBP'): string {
  return new Intl.NumberFormat('en-GB', {
    style: 'currency',
    currency,
  }).format(cents / 100)
}
</script>

<template>
  <div class="space-y-6">
    <div>
      <h1 class="text-3xl font-bold tracking-tight">Admin Dashboard</h1>
      <p class="text-muted-foreground">
        Platform overview and statistics
      </p>
    </div>

    <Alert v-if="error" variant="destructive">
      <AlertDescription>\{{ error }}</AlertDescription>
    </Alert>

    <div class="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
      <!-- Total Users -->
      <Card>
        <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle class="text-sm font-medium">Total Users</CardTitle>
          <svg class="h-4 w-4 text-muted-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
          </svg>
        </CardHeader>
        <CardContent>
          <Skeleton v-if="isLoading" class="h-8 w-20" />
          <div v-else class="text-2xl font-bold">\{{ stats?.total_users ?? 0 }}</div>
          <p class="text-xs text-muted-foreground">
            Registered accounts
          </p>
        </CardContent>
      </Card>

      <!-- Organizations -->
      <Card>
        <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle class="text-sm font-medium">Organizations</CardTitle>
          <svg class="h-4 w-4 text-muted-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
          </svg>
        </CardHeader>
        <CardContent>
          <Skeleton v-if="isLoading" class="h-8 w-20" />
          <div v-else class="text-2xl font-bold">\{{ stats?.total_organizations ?? 0 }}</div>
          <p class="text-xs text-muted-foreground">
            Active organizations
          </p>
        </CardContent>
      </Card>

      <!-- Active Subscriptions -->
      <Card>
        <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle class="text-sm font-medium">Subscriptions</CardTitle>
          <svg class="h-4 w-4 text-muted-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
          </svg>
        </CardHeader>
        <CardContent>
          <Skeleton v-if="isLoading" class="h-8 w-20" />
          <div v-else class="text-2xl font-bold">\{{ stats?.active_subscriptions ?? 0 }}</div>
          <p class="text-xs text-muted-foreground">
            Active subscriptions
          </p>
        </CardContent>
      </Card>

      <!-- MRR -->
      <Card>
        <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-2">
          <CardTitle class="text-sm font-medium">MRR</CardTitle>
          <svg class="h-4 w-4 text-muted-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
          </svg>
        </CardHeader>
        <CardContent>
          <Skeleton v-if="isLoading" class="h-8 w-24" />
          <div v-else class="text-2xl font-bold">\{{ formatCurrency(stats?.mrr_cents ?? 0) }}</div>
          <p class="text-xs text-muted-foreground">
            Monthly recurring revenue
          </p>
        </CardContent>
      </Card>
    </div>

    <!-- Recent Signups -->
    <Card>
      <CardHeader>
        <CardTitle>Recent Activity</CardTitle>
        <CardDescription>
          Overview of recent platform activity
        </CardDescription>
      </CardHeader>
      <CardContent>
        <div class="flex items-center gap-4">
          <div class="flex h-12 w-12 items-center justify-center rounded-full bg-primary/10">
            <svg class="h-6 w-6 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z" />
            </svg>
          </div>
          <div>
            <Skeleton v-if="isLoading" class="h-6 w-16" />
            <p v-else class="text-2xl font-bold">\{{ stats?.recent_signups ?? 0 }}</p>
            <p class="text-sm text-muted-foreground">New users in the last 30 days</p>
          </div>
        </div>
      </CardContent>
    </Card>
  </div>
</template>
