import { redirect } from "next/navigation";
import { createClient } from "@/lib/supabase/server";
import { LeaderboardView } from "./leaderboard-view";
import { QuickLinks } from "./quick-links";
import { GameHighlights, type GameLeader } from "./game-highlights";
import { AgoraLogo } from "@/components/agora-logo";
import { formatCountdown, formatTribunalDateTime } from "@/lib/tribunal-date";

export default async function LeaderboardPage() {
  const supabase = await createClient();
  const {
    data: { user },
  } = await supabase.auth.getUser();

  if (!user) {
    redirect("/login");
  }

  const [{ data: profiles, error }, { data: settings }, { data: wallNotesToday }, { data: icarusTop }, { data: melonTop }] =
    await Promise.all([
      supabase
        .from("profiles")
        .select("id, pseudo, avatar_url, points, previous_rank, role")
        .order("points", { ascending: false }),
      supabase.from("settings").select("tribunal_date").eq("id", true).single(),
      // wall_notes_for_date() (jamais une lecture directe de wall_notes, dont
      // la RLS ne laisserait voir que ses propres notes) : seul moyen de
      // connaître le nombre total de publications du jour sans exposer qui a
      // publié quoi — sert juste de compteur pour la pastille du raccourci.
      supabase.rpc("wall_notes_for_date"),
      supabase.from("icarus_scores").select("user_id, best_score").order("best_score", { ascending: false }).limit(1),
      supabase.from("melon_scores").select("user_id, best_score").order("best_score", { ascending: false }).limit(1),
    ]);

  const isJudge = profiles?.find((p) => p.id === user.id)?.role === "judge";
  const tribunalDate = settings?.tribunal_date ?? null;

  const profileById = new Map((profiles ?? []).map((p) => [p.id, p]));
  // Comme les podiums d'Icare/Corne : un meneur à 0 (personne n'a encore
  // joué) n'est pas un champion, pas de couronne pour un score par défaut.
  function buildLeader(rows: { user_id: string; best_score: number }[] | null): GameLeader {
    const top = rows?.[0];
    if (!top || top.best_score <= 0) return null;
    const profile = profileById.get(top.user_id);
    return { pseudo: profile?.pseudo ?? "Un Citoyen", avatarUrl: profile?.avatar_url ?? null, score: top.best_score };
  }
  const icarusLeader = buildLeader(icarusTop);
  const melonLeader = buildLeader(melonTop);

  return (
    <div className="mx-auto w-full max-w-2xl px-4 py-8">
      <div className="mb-6 flex flex-col items-center text-center">
        <AgoraLogo className="h-32 w-32 sm:h-40 sm:w-40" />
        <p className="mt-2 font-serif text-sm text-marble/60 italic">Le classement des Citoyens</p>
        <p className="mt-3 font-heading text-sm tracking-wide text-gold-bright uppercase">
          {formatCountdown(tribunalDate)}
        </p>
        {tribunalDate && <p className="text-xs text-marble/50">{formatTribunalDateTime(tribunalDate)}</p>}
      </div>

      <QuickLinks wallNoteCount={wallNotesToday?.length ?? 0} />

      <GameHighlights icarusLeader={icarusLeader} melonLeader={melonLeader} />

      {error && (
        <p role="alert" className="text-sm text-oxblood">
          Impossible de charger le classement pour le moment.
        </p>
      )}

      {!error && <LeaderboardView initialProfiles={profiles ?? []} isJudge={isJudge} />}
    </div>
  );
}
