<template>
  <div class="admin-page">
    <section class="card card-headerless">
      <div class="stats-grid">
        <div class="stat" v-for="item in statCards" :key="item.label">
          <div class="stat-label">{{ item.label }}</div>
          <div class="stat-value">{{ item.value }}</div>
          <div class="stat-desc">{{ item.desc }}</div>
        </div>
      </div>
      <div class="card-actions">
        <button class="btn primary" type="button" @click="refreshStats" :disabled="loading">
          {{ loading ? '刷新中...' : '刷新数据' }}
        </button>
      </div>
    </section>

    <section class="card">
      <div class="quick-links">
        <NuxtLink class="quick-link" to="/admin/users">
          <span>用户管理</span>
          <small>新增用户、分配角色、编辑信息</small>
        </NuxtLink>
        <NuxtLink class="quick-link" to="/admin/roles">
          <span>角色管理</span>
          <small>维护角色描述与权限列表</small>
        </NuxtLink>
        <NuxtLink class="quick-link" to="/admin/articles">
          <span>文章管理</span>
          <small>按标签/作者筛选并编辑文章</small>
        </NuxtLink>
        <NuxtLink class="quick-link" to="/admin/menu-tags">
          <span>菜单标签</span>
          <small>为资讯广场/使用教程分配标签</small>
        </NuxtLink>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, ref, watch, type Ref } from 'vue'
import { navigateTo } from '#app'
import { useAuth } from '@/composables/useAuth'
import { useAuthToken, useApi } from '@/composables/useApi'

definePageMeta({
  layout: 'admin',
  adminTitle: '仪表盘',
  adminSub: '站点后台 · 管理用户、角色与文章',
})

interface AdminDashboardStats {
  users: number
  roles: number
  articles: number
  published_today: number
  total_views: number
}

const { token } = useAuthToken()
const { user: authUser, fetchMe } = useAuth() as {
  user: Ref<{ roles?: string[] } | null>
  fetchMe: () => Promise<unknown>
}
const api = useApi()

const loading = ref(false)
const stats = ref<AdminDashboardStats | null>(null)
const hasAccess = ref(false)

const isAdmin = computed(() => {
  const roles = authUser.value?.roles
  return Array.isArray(roles) && roles.includes('admin')
})

async function ensureAccess(redirect = true): Promise<boolean> {
  if (!token.value) {
    hasAccess.value = false
    if (redirect) await navigateTo('/login')
    return false
  }
  if (!authUser.value) {
    try {
      await fetchMe()
    } catch (err) {
      console.error('[Admin] fetch user failed', err)
    }
  }
  const ok = isAdmin.value
  hasAccess.value = ok
  if (!ok && redirect) await navigateTo('/')
  return ok
}

async function loadStats(): Promise<void> {
  if (!hasAccess.value) return
  loading.value = true
  try {
    const res = (await api.get('/admin/dashboard')) as Partial<AdminDashboardStats>
    stats.value = {
      users: Number(res.users ?? 0),
      roles: Number(res.roles ?? 0),
      articles: Number(res.articles ?? 0),
      published_today: Number(res.published_today ?? 0),
      total_views: Number(res.total_views ?? 0),
    }
  } catch (err) {
    console.error('[Admin] load stats failed', err)
  } finally {
    loading.value = false
  }
}

async function refreshStats(): Promise<void> {
  await loadStats()
}

const statCards = computed(() => [
  { label: '用户总数', value: stats.value?.users ?? 0, desc: '含所有注册账号' },
  { label: '角色数量', value: stats.value?.roles ?? 0, desc: '可用角色个数' },
  { label: '文章总数', value: stats.value?.articles ?? 0, desc: '当前存量文章' },
  { label: '今日发布', value: stats.value?.published_today ?? 0, desc: '今日新增文章' },
  { label: '站点总浏览', value: stats.value?.total_views ?? 0, desc: '累积 PV' },
])

onMounted(async () => {
  if (await ensureAccess(true)) {
    await loadStats()
  }
})

watch(
  () => authUser.value?.roles,
  async () => {
    if (await ensureAccess(false)) {
      await loadStats()
    }
  },
)
</script>

<style scoped>
.admin-page {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: #ffffff;
  border-radius: 16px;
  border: 1px solid #e5e7eb;
  padding: 16px;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
}

.card-headerless {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card-actions {
  display: flex;
  justify-content: flex-end;
}

.btn {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid #d1d5db;
  background: #fff;
  cursor: pointer;
  transition: all 0.18s ease;
}

.btn.primary {
  background: #111827;
  color: #fff;
  border-color: #111827;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}

.stat {
  padding: 12px;
  border-radius: 12px;
  background: linear-gradient(135deg, #eef2ff, #f8fafc);
  border: 1px solid #e5e7eb;
}

.stat-label {
  color: #6b7280;
  font-size: 13px;
  margin-bottom: 6px;
}

.stat-value {
  font-size: 26px;
  font-weight: 800;
  color: #0f172a;
}

.stat-desc {
  margin-top: 4px;
  color: #9ca3af;
  font-size: 12px;
}

.quick-links {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
}

.quick-link {
  display: block;
  padding: 12px 14px;
  border-radius: 12px;
  text-decoration: none;
  background: #0f172a;
  color: #e5e7eb;
  border: 1px solid rgba(255, 255, 255, 0.08);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.quick-link small {
  display: block;
  margin-top: 4px;
  color: #cbd5e1;
  font-size: 12px;
}

.quick-link:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.25);
}
</style>
