/**
 * llmindex.io — home: hero + live global leaderboard + efficiency frontier
 * Author:  Simon-Pierre Boucher
 * Contact: contact@spboucher.ai
 * License: Proprietary — © Simon-Pierre Boucher, all rights reserved
 */
import Link from 'next/link';
import { DOMAINS, INDEX_VERSION } from '@llmindex/scoring';
import { LiveLeaderboard, type ApiEntry, type ApiRun } from '@/components/LiveLeaderboard';
import { EfficiencyLive, HomeStats, type EffPoint } from '@/components/EfficiencyLive';
import { getEfficiencyData, getLeaderboard } from '@/lib/data';

export const revalidate = 300;

export default async function HomePage() {
  const [data, efficiency] = await Promise.all([getLeaderboard('global'), getEfficiencyData()]);

  const initialRun: ApiRun | null = data
    ? {
        id: data.run.id,
        kind: data.run.kind,
        status: data.run.status,
        created_at: new Date(data.run.createdAt).toISOString(),
        notes: data.run.notes,
      }
    : null;
  const initialEntries: ApiEntry[] = (data?.entries ?? []).map((e) => ({
    rank: e.rank,
    model: e.slug,
    name: e.name,
    provider: e.provider,
    score: e.score,
    score_low: e.scoreLow,
    score_high: e.scoreHigh,
  }));
  const initialEfficiency: EffPoint[] = efficiency.map((p) => ({
    model: p.slug,
    name: p.name,
    score: p.score,
    cost_per_1k_items: p.costPer1kItems,
    on_frontier: p.onFrontier,
  }));

  return (
    <div className="space-y-10 sm:space-y-14">
      <section className="space-y-4">
        <h1 className="max-w-3xl text-3xl font-bold leading-tight tracking-tight text-zinc-900 sm:text-4xl">
          The{' '}
          <span className="bg-gradient-to-r from-emerald-600 to-cyan-500 bg-clip-text text-transparent">
            discriminative
          </span>{' '}
          LLM index
        </h1>
        <p className="max-w-3xl text-sm leading-relaxed text-zinc-600 sm:text-base">
          IRT-scored, contamination-resistant rankings on dynamically generated items — with
          confidence intervals, consistency, calibration, and an efficiency frontier. No saturated
          benchmarks, no leaked test sets. Results stream in live as each model finishes its
          evaluation.
        </p>
        <HomeStats
          initialCount={initialEntries.length}
          domains={DOMAINS.length}
          version={INDEX_VERSION}
        />
        <div className="flex flex-wrap gap-2 pt-1">
          {DOMAINS.map((d) => (
            <Link
              key={d}
              href={`/leaderboard/${d}`}
              className="rounded-full border border-zinc-300 px-3 py-1 text-xs capitalize text-zinc-700 transition-colors hover:border-emerald-400 hover:text-emerald-600"
            >
              {d.replaceAll('_', ' ')}
            </Link>
          ))}
        </div>
      </section>

      <section>
        <LiveLeaderboard
          initialRun={initialRun}
          initialEntries={initialEntries}
          indexVersion={data?.run.indexVersion ?? INDEX_VERSION}
        />
      </section>

      <EfficiencyLive initial={initialEfficiency} />
    </div>
  );
}
