import { auth } from '@/lib/auth' import { supabaseAdmin } from '@/lib/supabaseAdmin' import Link from 'next/link' function timeAgo(iso: string) { const diffMs = Date.now() - new Date(iso).getTime() const mins = Math.floor(diffMs / 60000) if (mins < 1) return 'az önce' if (mins < 60) return `${mins} dk önce` const hours = Math.floor(mins / 60) if (hours < 24) return `${hours} sa önce` const days = Math.floor(hours / 24) return `${days} gün önce` } export default async function AdminDashboardPage() { const session = await auth() const now = new Date().toISOString() const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString() const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString() const [ { count: totalUsers }, { count: activeLicenses }, { count: trialExpiringSoon }, { count: crashes24h }, { data: recentCrashes }, { data: recentLicenseEvents }, ] = await Promise.all([ supabaseAdmin.from('profiles').select('id', { count: 'exact', head: true }), supabaseAdmin.from('licenses').select('id', { count: 'exact', head: true }).eq('status', 'active'), supabaseAdmin.from('profiles').select('id', { count: 'exact', head: true }).eq('license_status', 'trial').gte('trial_end_date', now).lte('trial_end_date', new Date(Date.now() + 3 * 24 * 60 * 60 * 1000).toISOString()), supabaseAdmin.from('crash_reports').select('id', { count: 'exact', head: true }).gte('created_at', oneDayAgo), supabaseAdmin.from('crash_reports').select('id, error_message, severity, app_version, created_at').order('created_at', { ascending: false }).limit(5), supabaseAdmin.from('license_events').select('id, event_type, license_id, created_at').order('created_at', { ascending: false }).limit(5), ]) const activity = [ ...(recentCrashes || []).map((c) => ({ id: c.id, text: `Hata: ${c.error_message || c.severity} (v${c.app_version})`, created_at: c.created_at, tone: 'danger' as const })), ...(recentLicenseEvents || []).map((e) => ({ id: e.id, text: `Lisans olayı: ${e.event_type}`, created_at: e.created_at, tone: 'neutral' as const })), ].sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()).slice(0, 8) const stats = [ { name: 'Toplam Kullanıcı', stat: totalUsers ?? 0, href: '/admin/users' }, { name: 'Aktif Lisans', stat: activeLicenses ?? 0, href: '/admin/licenses' }, { name: 'Denemesi 3 Gün İçinde Bitecek', stat: trialExpiringSoon ?? 0, href: '/admin/users' }, { name: 'Son 24 Saatte Hata', stat: crashes24h ?? 0, href: '/admin/logs' }, ] return (
Hoş geldiniz, {session?.user?.name || session?.user?.email}. İşte AyrisLegal'ın canlı durumu.