import { cookies } from 'next/headers'
import { getIronSession } from 'iron-session'
import { SessionData, sessionOptions } from '@/lib/session'
import { prisma } from '@/lib/prisma'
import { TokenManager } from '@/components/settings/token-manager'
import Link from 'next/link'

export default async function TokensPage() {
  const session = await getIronSession<SessionData>(await cookies(), sessionOptions)

  const tokens = await prisma.apiToken.findMany({
    where: { user_id: session.userId },
    orderBy: { created_at: 'desc' },
  })

  return (
    <div className="p-6 max-w-2xl mx-auto w-full">
      <div className="flex items-center gap-3 mb-6">
        <Link href="/settings" className="text-muted-foreground hover:text-foreground text-sm">
          ← Instellingen
        </Link>
        <span className="text-muted-foreground">/</span>
        <h1 className="text-xl font-medium text-foreground">API Tokens</h1>
      </div>

      <TokenManager
        tokens={tokens.map(t => ({
          id: t.id,
          label: t.label,
          created_at: t.created_at.toISOString(),
          revoked_at: t.revoked_at?.toISOString() ?? null,
        }))}
        isDemo={session.isDemo ?? false}
      />
    </div>
  )
}
