import { notFound } from "next/navigation"

import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { prisma } from "@/lib/prisma"
import { normalizeInvite } from "@/server/invite-utils"
import { InviteAcceptForm } from "@/components/invite/invite-accept-form"

export const dynamic = "force-dynamic"

export default async function InvitePage({ params }: { params: Promise<{ token: string }> }) {
  const { token } = await params
  const invite = await prisma.authInvite.findUnique({
    where: { token },
    include: { events: { orderBy: { createdAt: "asc" } } },
  })

  if (!invite) {
    notFound()
  }

  const normalized = normalizeInvite(invite, new Date())
  const { events: unusedEvents, inviteUrl: unusedInviteUrl, ...summary } = normalized
  void unusedEvents
  void unusedInviteUrl

  return (
    <main className="mx-auto flex min-h-screen w-full max-w-3xl items-center justify-center px-4 py-16">
      <Card className="w-full border border-border/70 shadow-sm">
        <CardHeader className="space-y-2 text-center">
          <CardTitle className="text-2xl font-semibold text-neutral-900">Aceitar convite</CardTitle>
          <CardDescription className="text-sm text-neutral-600">
            Conclua seu cadastro para acessar a plataforma Sistema de chamados.
          </CardDescription>
        </CardHeader>
        <CardContent>
          <InviteAcceptForm invite={summary} />
        </CardContent>
      </Card>
    </main>
  )
}
