import { Link, useParams } from 'react-router-dom';
import { ArrowLeft, ArrowRight, FileText, Play, Quote, TriangleAlert } from 'lucide-react';

import { iconFor } from '@/content/icons';
import { demosForVertical, featuredDemo, lineup, routes } from '@/content/lineup';
import { PROPOSAL_NOTICE, verticalBySlug } from '@/content/verticals';
import * as s from '@/content/styles';
import { pageTitle, useSeo } from '@/lib/seo';

export default function VerticalPage() {
  const { slug } = useParams<{ slug: string }>();
  const vertical = verticalBySlug(slug);

  // Before the early return, because a hook cannot be called conditionally —
  // and because the "no such vertical" branch needs a head of its own too, or
  // it inherits whichever page the visitor came from.
  useSeo({
    title: pageTitle(vertical?.title ?? 'No such vertical'),
    description: vertical
      ? `${vertical.task} ${vertical.reward}`
      : 'There is no proposal at that address. The twelve we have written are all on the home page.',
    ...(slug ? { canonical: routes.vertical(slug) } : {}),
    ...(vertical ? { ogImage: `/og/vertical-${vertical.slug}.png` } : {}),
  });

  if (!vertical) {
    return (
      <main className={`${s.shell} py-16`}>
        <h1 className={s.h1}>No such vertical.</h1>
        <p className={`${s.lede} mt-4 max-w-xl`}>
          There is no proposal at <code className="font-mono text-fg">/verticals/{slug}</code>. The
          twelve we have written are all on the home page.
        </p>
        <div className="mt-6 flex flex-col gap-3 sm:flex-row">
          <Link className={s.btnPrimary} to={routes.home}>
            See the lineup
          </Link>
          <Link className={s.btnSecondary} to={routes.gallery}>
            See what is built
          </Link>
        </div>
      </main>
    );
  }

  const Icon = iconFor(vertical.icon);
  const demos = demosForVertical(vertical.key);
  const live = demos.find((d) => d.status === 'live');
  const spec = demos.find((d) => d.status === 'spec');

  const index = lineup.findIndex((v) => v.slug === vertical.slug);
  const previous = index > 0 ? lineup[index - 1] : undefined;
  const next = index >= 0 ? lineup[index + 1] : undefined;

  return (
    <main className={`${s.shell} py-10 sm:py-14`}>
      <Link
        className="tap inline-flex items-center gap-1.5 text-sm font-medium text-muted transition-colors duration-1 ease-enter hover:text-fg"
        to={routes.home}
      >
        <ArrowLeft aria-hidden="true" className="size-4" />
        All twelve
      </Link>

      <header className="mt-4">
        <div className="flex flex-wrap items-center gap-2">
          <span className={s.proposalPill}>{PROPOSAL_NOTICE}</span>
          <span className={s.pill}>Rank {vertical.rank} of {lineup.length}</span>
          <span className={s.pill}>
            {vertical.plannedForV1 ? 'In the first set' : 'Not in the first set'}
          </span>
        </div>
        <div className="mt-5 flex items-start gap-4">
          <span className="card grid size-12 shrink-0 place-items-center bg-surface-2">
            <Icon aria-hidden="true" className="size-6 text-brand" />
          </span>
          <h1 className={`${s.h1} min-w-0`}>{vertical.title}</h1>
        </div>
      </header>

      {/* ── Who this is for, and what is already worrying them ─────────── */}
      <section className="mt-8">
        <p className={s.eyebrow}>Who it is for</p>
        <p className="mt-2 text-lg font-semibold text-fg">{vertical.persona}</p>
        <blockquote className="card mt-4 max-w-3xl p-5">
          <Quote aria-hidden="true" className="size-5 text-muted" />
          <p className="mt-2 text-lg leading-relaxed text-fg">{vertical.anxiety}</p>
          <footer className="mt-3 text-sm text-muted">
            The question in the room before anyone opens a laptop.
          </footer>
        </blockquote>
      </section>

      {/* ── The environment, in the same three parts every time ────────── */}
      <section className="mt-10 grid gap-4 lg:grid-cols-3">
        <article className="card p-5">
          <h2 className={s.eyebrow}>The task</h2>
          <p className={`${s.prose} mt-2 text-[0.9375rem] text-fg`}>{vertical.task}</p>
        </article>
        <article className="card p-5">
          <h2 className={s.eyebrow}>The reward</h2>
          <p className={`${s.prose} mt-2 text-[0.9375rem] text-fg`}>{vertical.reward}</p>
        </article>
        <article className="card border-warning/40 p-5">
          <h2 className={`${s.eyebrow} text-warning`}>The counterweight</h2>
          <p className={`${s.prose} mt-2 text-[0.9375rem] text-fg`}>{vertical.counterweight}</p>
        </article>
      </section>

      <p className={`${s.prose} mt-4 max-w-3xl text-sm`}>
        The counterweight is the part worth arguing about. A reward with nothing pulling against it
        is a target, and a target is what a model learns to hit instead of doing the job.
      </p>

      {vertical.caveat ? (
        <section className="card mt-8 border-warning/40 bg-warning/5 p-5 sm:p-6">
          <h2 className={`${s.h3} flex items-center gap-2 text-warning`}>
            <TriangleAlert aria-hidden="true" className="size-4 shrink-0" />
            Where this stops
          </h2>
          <p className={`${s.prose} mt-2 max-w-3xl text-fg`}>{vertical.caveat}</p>
        </section>
      ) : null}

      {/* ── What actually exists for this vertical, stated plainly ─────── */}
      <section className="mt-10">
        <h2 className={s.h2}>What exists today</h2>
        {live ? (
          <Link
            aria-label={`${live.title} — play it`}
            className={`${s.cardLink} mt-4`}
            to={routes.demo(live.slug)}
          >
            <span className={`${s.pill} self-start border-positive/30 bg-positive/10 text-positive`}>
              Live demo
            </span>
            <span className="mt-3 text-xl font-bold tracking-tight text-fg">{live.title}</span>
            <span className={`${s.prose} mt-1.5`}>{live.tagline}</span>
            <span className="mt-4 inline-flex items-center gap-1.5 text-sm font-semibold text-accent-fg">
              <Play aria-hidden="true" className="size-4" />
              Play it
            </span>
          </Link>
        ) : spec ? (
          <Link
            aria-label={`${spec.title} — read the specification`}
            className={`${s.cardLink} mt-4`}
            to={routes.demo(spec.slug)}
          >
            <span className={`${s.pill} self-start gap-1`}>
              <FileText aria-hidden="true" className="size-3.5" />
              Published specification
            </span>
            <span className="mt-3 text-xl font-bold tracking-tight text-fg">{spec.title}</span>
            <span className={`${s.prose} mt-1.5`}>{spec.tagline}</span>
            <span className="mt-4 inline-flex items-center gap-1.5 text-sm font-semibold text-accent-fg">
              Read the specification
              <ArrowRight aria-hidden="true" className="size-4" />
            </span>
          </Link>
        ) : (
          <div className="card mt-4 p-5 sm:p-6">
            <p className={`${s.prose} max-w-3xl text-fg`}>
              Nothing. There is no environment for this vertical, no recorded run, and no number. It
              is a proposal, written to the same shape as the one that is built so you can judge it
              on the same terms.
            </p>
            {featuredDemo ? (
              <p className={`${s.prose} mt-3 max-w-3xl`}>
                The environment that does exist is{' '}
                <Link className={s.link} to={routes.demo(featuredDemo.slug)}>
                  {featuredDemo.title}
                </Link>
                . It is a word game, and it is deliberately not one of these twelve: it is there to
                show the machinery, not to imply we have built yours.
              </p>
            ) : null}
            <div className="mt-5 flex flex-col gap-3 sm:flex-row">
              <Link className={s.btnSecondary} to={routes.gallery}>
                See what is built
              </Link>
              <Link className={s.btnSecondary} to={routes.honesty}>
                What we have not measured
              </Link>
            </div>
          </div>
        )}
      </section>

      {/* ── Move along the ranking ─────────────────────────────────────── */}
      <nav aria-label="Other verticals" className="mt-12 grid gap-3 sm:grid-cols-2">
        {previous ? (
          <Link
            aria-label={`Previous: ${previous.title}`}
            className={`${s.cardLink} sm:items-start`}
            to={routes.vertical(previous.slug)}
          >
            <span className="inline-flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wider text-muted">
              <ArrowLeft aria-hidden="true" className="size-3.5" />
              Rank {previous.rank}
            </span>
            <span className={`${s.h3} mt-1.5`}>{previous.title}</span>
          </Link>
        ) : (
          <span />
        )}
        {next ? (
          <Link
            aria-label={`Next: ${next.title}`}
            className={`${s.cardLink} sm:col-start-2 sm:items-end sm:text-right`}
            to={routes.vertical(next.slug)}
          >
            <span className="inline-flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wider text-muted">
              Rank {next.rank}
              <ArrowRight aria-hidden="true" className="size-3.5" />
            </span>
            <span className={`${s.h3} mt-1.5`}>{next.title}</span>
          </Link>
        ) : null}
      </nav>
    </main>
  );
}
