import Link from "next/link";
import { getCurrentUser } from "@/lib/auth";
import { Badge, btn, Card, Field, Logo, input } from "@/components/ui";
import { CONFERENCE_CATEGORIES, CONFERENCE_TYPES } from "@/lib/constants";
import { LocationPicker } from "@/components/location-picker";
import { startConferenceAction } from "./actions";
import { OnboardingSteps } from "./steps";

export const metadata = { title: "Start your conference — free" };

const PERKS: [string, string][] = [
  ["🌐", "A public conference website, live the moment you publish"],
  ["🎟️", "Free registration engine with QR tickets & check-in"],
  ["🗓️", "Agenda builder with tracks, halls and drag-reorder"],
  ["🎤", "Speaker pages — even import profiles from LinkedIn"],
  ["📺", "A live conference screen with polls, Q&A and reactions"],
  ["🏅", "One-click participation certificates with public verification"],
];

export default async function StartPage({ searchParams }: { searchParams: Promise<{ error?: string }> }) {
  const user = await getCurrentUser();
  const { error } = await searchParams;

  return (
    <main className="min-h-screen pb-16">
      <header className="mx-auto flex max-w-6xl items-center justify-between px-4 py-5">
        <Logo />
        <div className="text-sm text-zinc-500">
          {user ? (
            <span>Signed in as <b className="text-zinc-300">{user.name}</b></span>
          ) : (
            <>Already organizing? <Link href="/login" className="font-semibold text-indigo-600 hover:underline">Sign in</Link></>
          )}
        </div>
      </header>

      <div className="mx-auto max-w-6xl px-4">
        <div className="text-center">
          <Badge className="fx-in border border-indigo-200/70 bg-white/60 text-indigo-600 shadow-sm backdrop-blur">🚀 Free to start · no card needed</Badge>
          <h1 className="fx-in fx-d1 mt-4 text-3xl font-extrabold tracking-tight text-zinc-50 sm:text-4xl">
            Start your first conference
          </h1>
          <p className="fx-in fx-d2 mx-auto mt-2 max-w-xl text-zinc-400">
            Tell us the basics — {user ? "your workspace opens right after." : "then verify your email and your organizer dashboard is ready."}
          </p>
          <div className="mt-6"><OnboardingSteps active={1} /></div>
        </div>

        {error && (
          <p className="fx-in mx-auto mt-6 max-w-2xl rounded-xl bg-red-500/10 px-4 py-2.5 text-center text-sm font-medium text-red-600">{error}</p>
        )}

        <div className="mt-8 grid gap-8 lg:grid-cols-[1fr_320px]">
          {/* Basics form */}
          <Card className="fx-in fx-d2 p-6 sm:p-8">
            <form action={startConferenceAction} className="space-y-5">
              <div className="grid gap-4 sm:grid-cols-2">
                <div className="sm:col-span-2">
                  <Field name="name" title="Conference name *">
                    <input id="name" name="name" required minLength={3} placeholder="India AI Conference 2027" className={input} />
                  </Field>
                </div>
                <Field name="type" title="Format *">
                  <select id="type" name="type" className={input} defaultValue="PHYSICAL">
                    {CONFERENCE_TYPES.map((t) => <option key={t} value={t}>{t === "PHYSICAL" ? "In-person" : t === "VIRTUAL" ? "Online" : "Hybrid"}</option>)}
                  </select>
                </Field>
                <Field name="category" title="Category">
                  <select id="category" name="category" className={input} defaultValue="TECH">
                    {CONFERENCE_CATEGORIES.map((c) => <option key={c} value={c}>{c[0] + c.slice(1).toLowerCase()}</option>)}
                  </select>
                </Field>
                <Field name="startAt" title="Start date *"><input id="startAt" name="startAt" type="date" required className={input} /></Field>
                <Field name="endAt" title="End date *"><input id="endAt" name="endAt" type="date" required className={input} /></Field>
              </div>

              <Field name="tagline" title="Tagline">
                <input id="tagline" name="tagline" placeholder="One line that sells your conference" className={input} />
              </Field>
              <Field name="description" title="Short description">
                <textarea id="description" name="description" rows={3} placeholder="What's it about, who should attend…" className={input} />
              </Field>

              <div>
                <div className="mb-2 text-xs font-semibold uppercase tracking-wider text-zinc-500">Location</div>
                <LocationPicker />
              </div>
              <div className="grid gap-4 sm:grid-cols-3">
                <div className="sm:col-span-2">
                  <Field name="venueName" title="Venue (you can decide later)">
                    <input id="venueName" name="venueName" placeholder="Convention centre, hotel, campus…" className={input} />
                  </Field>
                </div>
                <Field name="capacity" title="Expected attendees">
                  <input id="capacity" name="capacity" type="number" min="1" placeholder="200" className={input} />
                </Field>
              </div>
              <Field name="primaryColor" title="Brand color">
                <div className="flex items-center gap-2.5">
                  <input id="primaryColor" name="primaryColor" type="color" defaultValue="#4f46e5" className="h-10 w-16 cursor-pointer rounded-xl border border-white/80 bg-white/70" />
                  <span className="text-xs text-zinc-500">Used across your website, tickets and live screen.</span>
                </div>
              </Field>

              <button className={`${btn.primary} w-full py-3 text-base`}>
                {user ? "Create my conference →" : "Continue — verify email →"}
              </button>
              <p className="text-center text-xs text-zinc-600">
                Your conference starts as a private draft — nothing goes public until you hit Publish.
              </p>
            </form>
          </Card>

          {/* What you get */}
          <div className="fx-in fx-d3 space-y-3">
            <h3 className="text-sm font-bold uppercase tracking-wider text-zinc-500">Included free</h3>
            {PERKS.map(([icon, text]) => (
              <Card key={text} className="flex items-start gap-3 p-3.5">
                <span className="text-lg">{icon}</span>
                <span className="text-sm text-zinc-400">{text}</span>
              </Card>
            ))}
            <p className="pt-1 text-xs text-zinc-600">
              Trusted workflow: Registration → QR Ticket → Check-in → Certificate, all connected.
            </p>
          </div>
        </div>
      </div>
    </main>
  );
}
