<script setup lang="ts">
definePageMeta({ layout: false })

useHead({ title: 'Dashboard — vat-api.eu Admin' })

interface Stats {
  totalRequests: { last24h: number; last7d: number; last30d: number }
  uniqueIPs: number
  topIPs: { ip: string; count: number; lastSeen: string }[]
  requestsByHour: { hour: string; count: number }[]
}

interface RequestsResponse {
  requests: { ip: string; path: string; method: string; statusCode: number; userAgent: string; timestamp: string }[]
  total: number
  page: number
  limit: number
  totalPages: number
}

const stats = ref<Stats | null>(null)
const reqs = ref<RequestsResponse | null>(null)
const error = ref('')
const page = ref(1)
const ipFilter = ref('')
let refreshTimer: ReturnType<typeof setInterval> | null = null

async function fetchStats() {
  try {
    stats.value = await $fetch<Stats>('/api/admin/stats')
  } catch (e: any) {
    if (e?.response?.status === 401) return navigateTo('/admin')
    error.value = 'Failed to load stats'
  }
}

async function fetchRequests() {
  try {
    const params: Record<string, string | number> = { page: page.value, limit: 50 }
    if (ipFilter.value) params.ip = ipFilter.value
    reqs.value = await $fetch<RequestsResponse>('/api/admin/requests', { params })
  } catch (e: any) {
    if (e?.response?.status === 401) return navigateTo('/admin')
  }
}

function filterByIp(ip: string) {
  ipFilter.value = ip
  page.value = 1
  fetchRequests()
}

function clearFilter() {
  ipFilter.value = ''
  page.value = 1
  fetchRequests()
}

function changePage(p: number) {
  page.value = p
  fetchRequests()
}

function formatTime(ts: string) {
  return new Date(ts).toLocaleString()
}

function reqsPerMinute() {
  if (!stats.value) return '0'
  return (stats.value.totalRequests.last24h / 1440).toFixed(1)
}

async function refresh() {
  await Promise.all([fetchStats(), fetchRequests()])
}

onMounted(() => {
  refresh()
  refreshTimer = setInterval(refresh, 30_000)
})

onUnmounted(() => {
  if (refreshTimer) clearInterval(refreshTimer)
})
</script>

<template>
  <div class="min-h-screen bg-surface-soft">
    <!-- Header -->
    <header class="bg-white border-b border-surface-border">
      <div class="section-container flex items-center justify-between h-16">
        <div class="flex items-center gap-3">
          <NuxtLink to="/" class="flex items-center gap-2 group">
            <span class="inline-flex items-center justify-center w-8 h-8 rounded-lg bg-eu-blue text-white font-heading font-bold text-sm transition-transform group-hover:scale-105">
              V
            </span>
            <span class="font-heading font-bold text-lg text-ink">
              vat-api<span class="text-eu-blue">.eu</span>
            </span>
          </NuxtLink>
          <span class="text-ink-faint mx-1">/</span>
          <span class="text-sm font-medium text-ink-secondary">Admin</span>
        </div>
        <NuxtLink
          to="/admin"
          class="text-sm text-ink-muted hover:text-ink transition-colors"
        >
          Sign out
        </NuxtLink>
      </div>
    </header>

    <main class="section-container py-8">
      <div v-if="error" class="mb-6 px-4 py-3 rounded-xl bg-red-50 border border-red-200 text-red-700 text-sm">
        {{ error }}
      </div>

      <!-- Summary cards -->
      <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
        <div class="bg-white rounded-2xl border border-surface-border shadow-soft p-5">
          <p class="text-xs font-medium text-ink-muted uppercase tracking-wide">Requests (24h)</p>
          <p class="mt-1 text-2xl font-bold text-ink font-heading tabular-nums">
            {{ stats?.totalRequests.last24h?.toLocaleString() ?? '...' }}
          </p>
        </div>
        <div class="bg-white rounded-2xl border border-surface-border shadow-soft p-5">
          <p class="text-xs font-medium text-ink-muted uppercase tracking-wide">Requests (7d)</p>
          <p class="mt-1 text-2xl font-bold text-ink font-heading tabular-nums">
            {{ stats?.totalRequests.last7d?.toLocaleString() ?? '...' }}
          </p>
        </div>
        <div class="bg-white rounded-2xl border border-surface-border shadow-soft p-5">
          <p class="text-xs font-medium text-ink-muted uppercase tracking-wide">Unique IPs (24h)</p>
          <p class="mt-1 text-2xl font-bold text-ink font-heading tabular-nums">
            {{ stats?.uniqueIPs?.toLocaleString() ?? '...' }}
          </p>
        </div>
        <div class="bg-white rounded-2xl border border-surface-border shadow-soft p-5">
          <p class="text-xs font-medium text-ink-muted uppercase tracking-wide">Reqs / min (avg)</p>
          <p class="mt-1 text-2xl font-bold text-ink font-heading tabular-nums">
            {{ stats ? reqsPerMinute() : '...' }}
          </p>
        </div>
      </div>

      <div class="grid lg:grid-cols-2 gap-6 mb-8">
        <!-- Top IPs -->
        <div class="bg-white rounded-2xl border border-surface-border shadow-soft overflow-hidden">
          <div class="px-5 py-4 border-b border-surface-border bg-surface-soft">
            <h2 class="text-sm font-semibold text-ink font-heading">Top IPs (24h)</h2>
          </div>
          <div class="overflow-x-auto">
            <table class="w-full text-sm">
              <thead>
                <tr class="border-b border-surface-border">
                  <th class="text-left font-semibold text-ink-secondary px-5 py-2.5 font-heading">IP</th>
                  <th class="text-right font-semibold text-ink-secondary px-5 py-2.5 font-heading w-24">Count</th>
                  <th class="text-right font-semibold text-ink-secondary px-5 py-2.5 font-heading w-44">Last seen</th>
                </tr>
              </thead>
              <tbody>
                <tr
                  v-for="row in stats?.topIPs"
                  :key="row.ip"
                  class="border-b border-surface-border/60 last:border-0 hover:bg-eu-blue-100/40 transition-colors cursor-pointer"
                  @click="filterByIp(row.ip)"
                >
                  <td class="px-5 py-2.5">
                    <span class="font-mono text-xs text-ink">{{ row.ip }}</span>
                  </td>
                  <td class="px-5 py-2.5 text-right">
                    <span class="font-semibold text-ink tabular-nums">{{ row.count.toLocaleString() }}</span>
                  </td>
                  <td class="px-5 py-2.5 text-right text-ink-muted text-xs">
                    {{ formatTime(row.lastSeen) }}
                  </td>
                </tr>
                <tr v-if="!stats?.topIPs?.length">
                  <td colspan="3" class="px-5 py-8 text-center text-ink-muted">No data yet</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>

        <!-- Requests by hour -->
        <div class="bg-white rounded-2xl border border-surface-border shadow-soft overflow-hidden">
          <div class="px-5 py-4 border-b border-surface-border bg-surface-soft">
            <h2 class="text-sm font-semibold text-ink font-heading">Requests by Hour (24h)</h2>
          </div>
          <div class="p-5">
            <div v-if="stats?.requestsByHour?.length" class="flex items-end gap-1 h-40">
              <div
                v-for="bar in stats.requestsByHour"
                :key="bar.hour"
                class="flex-1 flex flex-col items-center gap-1"
              >
                <span class="text-[10px] text-ink-muted tabular-nums">{{ bar.count }}</span>
                <div
                  class="w-full bg-eu-blue/80 rounded-t transition-all"
                  :style="{ height: `${Math.max(4, (bar.count / Math.max(...stats.requestsByHour.map(b => b.count))) * 120)}px` }"
                />
                <span class="text-[10px] text-ink-faint tabular-nums">{{ bar.hour }}</span>
              </div>
            </div>
            <p v-else class="text-center text-ink-muted py-8">No data yet</p>
          </div>
        </div>
      </div>

      <!-- Recent requests -->
      <div class="bg-white rounded-2xl border border-surface-border shadow-soft overflow-hidden">
        <div class="px-5 py-4 border-b border-surface-border bg-surface-soft flex items-center justify-between">
          <h2 class="text-sm font-semibold text-ink font-heading">
            Recent Requests
            <span v-if="ipFilter" class="ml-2 text-xs font-normal text-ink-muted">
              filtered: {{ ipFilter }}
              <button class="ml-1 text-eu-blue hover:underline" @click="clearFilter">clear</button>
            </span>
          </h2>
          <span class="text-xs text-ink-muted tabular-nums">
            {{ reqs?.total?.toLocaleString() ?? '...' }} total
          </span>
        </div>
        <div class="overflow-x-auto">
          <table class="w-full text-sm">
            <thead>
              <tr class="border-b border-surface-border">
                <th class="text-left font-semibold text-ink-secondary px-5 py-2.5 font-heading">IP</th>
                <th class="text-left font-semibold text-ink-secondary px-5 py-2.5 font-heading">Method</th>
                <th class="text-left font-semibold text-ink-secondary px-5 py-2.5 font-heading">Path</th>
                <th class="text-center font-semibold text-ink-secondary px-5 py-2.5 font-heading w-20">Status</th>
                <th class="text-right font-semibold text-ink-secondary px-5 py-2.5 font-heading w-44">Time</th>
              </tr>
            </thead>
            <tbody>
              <tr
                v-for="(req, i) in reqs?.requests"
                :key="i"
                class="border-b border-surface-border/60 last:border-0 hover:bg-eu-blue-100/40 transition-colors"
              >
                <td class="px-5 py-2.5">
                  <button
                    class="font-mono text-xs text-eu-blue hover:underline"
                    @click="filterByIp(req.ip)"
                  >
                    {{ req.ip }}
                  </button>
                </td>
                <td class="px-5 py-2.5">
                  <span class="inline-flex px-2 py-0.5 rounded bg-surface-muted font-mono text-xs font-medium text-ink-secondary">
                    {{ req.method }}
                  </span>
                </td>
                <td class="px-5 py-2.5 font-mono text-xs text-ink">{{ req.path }}</td>
                <td class="px-5 py-2.5 text-center">
                  <span
                    class="inline-flex px-2 py-0.5 rounded text-xs font-semibold tabular-nums"
                    :class="req.statusCode < 400 ? 'bg-green-50 text-green-700' : 'bg-red-50 text-red-700'"
                  >
                    {{ req.statusCode }}
                  </span>
                </td>
                <td class="px-5 py-2.5 text-right text-ink-muted text-xs">
                  {{ formatTime(req.timestamp) }}
                </td>
              </tr>
              <tr v-if="!reqs?.requests?.length">
                <td colspan="5" class="px-5 py-8 text-center text-ink-muted">No requests yet</td>
              </tr>
            </tbody>
          </table>
        </div>

        <!-- Pagination -->
        <div v-if="reqs && reqs.totalPages > 1" class="px-5 py-3 border-t border-surface-border flex items-center justify-between">
          <button
            :disabled="page <= 1"
            class="text-sm text-eu-blue hover:underline disabled:text-ink-faint disabled:no-underline"
            @click="changePage(page - 1)"
          >
            Previous
          </button>
          <span class="text-xs text-ink-muted tabular-nums">
            Page {{ reqs.page }} of {{ reqs.totalPages }}
          </span>
          <button
            :disabled="page >= reqs.totalPages"
            class="text-sm text-eu-blue hover:underline disabled:text-ink-faint disabled:no-underline"
            @click="changePage(page + 1)"
          >
            Next
          </button>
        </div>
      </div>

      <p class="text-center text-xs text-ink-faint mt-6">
        Auto-refreshes every 30 seconds.
      </p>
    </main>
  </div>
</template>
