import Link from 'next/link' import { supabaseAdmin, listAuthUsers } from '@/lib/supabaseAdmin' interface LicenseRow { id: string customer_id: string plan: string status: string max_devices: number expires_at: string | null created_at: string } function statusBadge(status: string) { const map: Record = { active: 'bg-green-50 text-green-700 border-green-200 dark:bg-green-950/40 dark:text-green-400 dark:border-green-900', expired: 'bg-red-50 text-red-700 border-red-200 dark:bg-red-950/40 dark:text-red-400 dark:border-red-900', revoked: 'bg-gray-50 text-gray-600 border-gray-200 dark:bg-gray-900 dark:text-gray-400 dark:border-gray-800', } const cls = map[status] || 'bg-gray-50 text-gray-600 border-gray-200 dark:bg-gray-900 dark:text-gray-400 dark:border-gray-800' return {status} } function formatDate(iso: string | null) { if (!iso) return '—' return new Date(iso).toLocaleDateString('tr-TR', { day: 'numeric', month: 'short', year: 'numeric' }) } export default async function LicensesPage({ searchParams }: { searchParams: Promise<{ deleted?: string }> }) { const { deleted } = await searchParams const [{ data: licenses }, { data: activations }, { data: profiles }, authUsers] = await Promise.all([ supabaseAdmin.from('licenses').select('id, customer_id, plan, status, max_devices, expires_at, created_at').order('created_at', { ascending: false }), supabaseAdmin.from('license_activations').select('id, license_id, device_name, platform, last_seen_at'), supabaseAdmin.from('profiles').select('id, full_name'), listAuthUsers(), ]) const profileNameById = new Map((profiles || []).map((p: { id: string; full_name: string | null }) => [p.id, p.full_name])) const activationsByLicense = new Map() for (const a of activations || []) { const list = activationsByLicense.get(a.license_id) || [] list.push(a) activationsByLicense.set(a.license_id, list) } const rows = (licenses as LicenseRow[] | null) || [] return (
{deleted && (
Lisans silindi.
)}

Lisanslar

Toplam {rows.length} lisans.

+ Yeni Lisans Oluştur
{['Müşteri', 'Plan', 'Durum', 'Cihazlar', 'Bitiş', 'Oluşturma', ''].map((h) => ( ))} {rows.map((l) => { const devices = activationsByLicense.get(l.id) || [] const customerName = l.customer_id ? profileNameById.get(l.customer_id) : null const customerEmail = l.customer_id ? authUsers.get(l.customer_id)?.email : null return ( ) })} {rows.length === 0 && ( )}
{h}
{l.customer_id ? ( <>
{customerName || '(isimsiz)'}
{customerEmail || l.customer_id}
) : ( )}
{l.plan} {statusBadge(l.status)} {devices.length} / {l.max_devices} {devices.length > 0 && (
{devices.map((d) => d.platform).filter(Boolean).join(', ')}
)}
{formatDate(l.expires_at)} {formatDate(l.created_at)} Düzenle
Henüz lisans yok.
) }