import type { Metadata } from "next"
import Link from "next/link"
import { Suspense } from "react"

export const metadata: Metadata = {
  title: "Subscription confirmed — Parallax",
}

type ConfirmedContentProps = {
  searchParams: Promise<{
    status?: string
  }>
}

function getContent(status?: string) {
  if (status === "success") {
    return {
      icon: "✓",
      iconClass: "bg-[#14532d] text-[#4ade80]",
      title: "You're subscribed.",
      body:
        "Welcome to Parallax. You will receive weekly geopolitical analysis directly in your inbox. No noise. Just the forces shaping the world.",
      href: "/directory",
      label: "Browse reports",
    }
  }

  if (status === "already") {
    return {
      icon: "i",
      iconClass: "bg-[var(--color-tag-bg)] text-[var(--color-accent)]",
      title: "Already subscribed.",
      body: "This email address is already confirmed on Parallax.",
      href: "/",
      label: "Go to homepage",
    }
  }

  return {
    icon: "×",
    iconClass: "bg-[#450a0a] text-[#ef4444]",
    title: "Link expired.",
    body:
      "This confirmation link has already been used or is no longer valid. Subscribe again below.",
    href: "/",
    label: "Back to homepage",
  }
}

async function ConfirmedContent({ searchParams }: ConfirmedContentProps) {
  const { status } = await searchParams
  const content = getContent(status)

  return (
    <div className="w-full max-w-[480px] rounded-xl border border-[var(--color-border)] bg-[var(--color-surface)] p-10 text-center">
      <div
        className={`mx-auto mb-6 flex h-14 w-14 items-center justify-center rounded-full text-2xl ${content.iconClass}`}
      >
        {content.icon}
      </div>
      <h1 className="mt-0 font-serif text-[1.8rem] font-normal text-[var(--color-text-primary)]">
        {content.title}
      </h1>
      <p className="mt-3 text-[14px] leading-[1.7] text-[var(--color-text-secondary)]">
        {content.body}
      </p>
      <Link
        href={content.href}
        className="mt-6 inline-block rounded bg-[var(--color-accent)] px-6 py-3 text-sm font-medium text-white transition hover:bg-[var(--color-accent-hover)]"
      >
        {content.label}
      </Link>
    </div>
  )
}

export default function NewsletterConfirmedPage({
  searchParams,
}: ConfirmedContentProps) {
  return (
    <main className="flex min-h-screen items-center justify-center bg-[var(--color-bg)] px-4">
      <Suspense fallback={null}>
        <ConfirmedContent searchParams={searchParams} />
      </Suspense>
    </main>
  )
}
