"use client"

import type React from "react"
import { useEffect, useMemo, useState } from "react"
import Link from "next/link"
import {
  ArrowLeft,
  Check,
  ChevronLeft,
  ChevronRight,
  ChevronsUpDown,
  RefreshCw,
  Trophy,
  WifiOff,
} from "lucide-react"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { enqueueWrite, flushQueue, queueCount } from "@/lib/offline-queue"

// --- Types (matcher API-kontrakten i app/routers/tournaments.py/matches.py/scoring.py/courses.py)

type Format = "foursome" | "greensome" | "scramble_2" | "scramble_4" | "fourball" | "singles"
type ScoringMode = "stroke" | "hole_result"
type HoleConfig = "full_18" | "front_9" | "back_9"

const INDIVIDUAL_FORMATS: Format[] = ["singles", "fourball"]

const FORMAT_LABELS: Record<Format, string> = {
  foursome: "Foursome",
  greensome: "Greensome",
  scramble_2: "Scramble (2)",
  scramble_4: "Scramble (4)",
  fourball: "Fourball",
  singles: "Singel",
}

type ApiTeam = { id: string; name: string; color: string | null }

type ApiSession = {
  id: string
  name: string | null
  format: Format
  hole_config: HoleConfig
  course_id: string
  scoring_mode: ScoringMode
}

type ApiParticipant = {
  id: string
  team_side: "a" | "b"
  team_roster_id: string
  player_name: string
  tee_id: string
  tee_name: string
}

type ApiMatch = {
  id: string
  sequence: number
  team_a_id: string
  team_b_id: string
  participants: ApiParticipant[]
}

type ApiHole = { hole_number: number; par: number; stroke_index: number }

type ApiHoleScore = {
  hole_number: number
  team_side: "a" | "b"
  match_participant_id: string | null
  gross_strokes: number
}

type ApiHoleResult = { hole_number: number; winning_side: "a" | "b" | null }

type ApiScorecard = {
  match_id: string
  status_text: string | null
  points_side_a: number | null
  points_side_b: number | null
  holes: { hole_number: number; result: "a" | "b" | "halved" }[]
  stroke_entries: ApiHoleScore[] | null
  hole_result_entries: ApiHoleResult[] | null
}

// Enhet som scores: én per SPILLER for individuelle formater (singles/
// fourball), én per SIDE for delt-ball-formater (foursome/greensome/
// scramble) -- se app/routers/scoring.py sin is_individual-sjekk.
type Unit = { id: string; side: "a" | "b"; label: string; matchParticipantId: string | null }

// Nøkkel for det lokale offline-overlayet (ADR-028) -- entydig per
// hull+side+enhet, samme identitet som backend bruker for upsert.
function strokeKey(holeNumber: number, unit: Unit): string {
  return `${holeNumber}:${unit.side}:${unit.matchParticipantId ?? ""}`
}

function playedHoleNumbers(holeConfig: HoleConfig): number[] {
  if (holeConfig === "front_9") return Array.from({ length: 9 }, (_, i) => i + 1)
  if (holeConfig === "back_9") return Array.from({ length: 9 }, (_, i) => i + 10)
  return Array.from({ length: 18 }, (_, i) => i + 1)
}

export function SessionScorecard({
  organizationId,
  tournamentId,
  sessionId,
  matchId,
  tournamentName,
}: {
  organizationId: string
  tournamentId: string
  sessionId: string
  matchId: string
  tournamentName: string
}) {
  const [session, setSession] = useState<ApiSession | null>(null)
  const [teams, setTeams] = useState<[ApiTeam, ApiTeam] | null>(null)
  const [match, setMatch] = useState<ApiMatch | null>(null)
  const [holes, setHoles] = useState<ApiHole[]>([])
  const [scorecard, setScorecard] = useState<ApiScorecard | null>(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)
  const [currentHole, setCurrentHole] = useState<number | null>(null)
  const [showSummary, setShowSummary] = useState(false)
  const [showConcede, setShowConcede] = useState(false)
  const [concedeTeamIndex, setConcedeTeamIndex] = useState<0 | 1 | null>(null)

  // --- Offline scoreregistrering (ADR-028) -----------------------------
  // isOnline styrer kun banner-teksten (navigator.onLine er allerede
  // sjekket direkte der det faktisk betyr noe, i submitStroke/
  // submitHoleResult) -- pendingStrokes/pendingResults er et lokalt
  // overlay som viser verdier som er lagret i IndexedDB-køen, men ikke
  // bekreftet mot serveren ennå.
  const [isOnline, setIsOnline] = useState(true)
  const [pendingCount, setPendingCount] = useState(0)
  const [syncing, setSyncing] = useState(false)
  const [pendingStrokes, setPendingStrokes] = useState<Map<string, number>>(new Map())
  const [pendingResults, setPendingResults] = useState<Map<number, "a" | "b" | null>>(new Map())

  async function flushPending() {
    if (syncing) return
    setSyncing(true)
    try {
      const outcomes = await flushQueue(matchId)
      if (outcomes.length === 0) return
      setPendingStrokes((prev) => {
        const next = new Map(prev)
        for (const o of outcomes) {
          if (o.entry.url.endsWith("/hole-scores")) {
            const b = o.entry.body as {
              hole_number: number
              team_side: "a" | "b"
              match_participant_id: string | null
            }
            next.delete(`${b.hole_number}:${b.team_side}:${b.match_participant_id ?? ""}`)
          }
        }
        return next
      })
      setPendingResults((prev) => {
        const next = new Map(prev)
        for (const o of outcomes) {
          if (o.entry.url.endsWith("/hole-results")) {
            const b = o.entry.body as { hole_number: number }
            next.delete(b.hole_number)
          }
        }
        return next
      })
      const failed = outcomes.filter((o) => !o.ok)
      if (failed.length > 0) {
        setError(
          `${failed.length} lagret ${failed.length === 1 ? "endring" : "endringer"} kunne ikke synkroniseres: ${failed[0].message}`,
        )
      }
      setPendingCount(await queueCount(matchId))
      await refetchScorecard()
    } finally {
      setSyncing(false)
    }
  }

  useEffect(() => {
    setIsOnline(navigator.onLine)
    function handleOnline() {
      setIsOnline(true)
      void flushPending()
    }
    function handleOffline() {
      setIsOnline(false)
    }
    window.addEventListener("online", handleOnline)
    window.addEventListener("offline", handleOffline)
    return () => {
      window.removeEventListener("online", handleOnline)
      window.removeEventListener("offline", handleOffline)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [matchId])

  async function queueStroke(holeNumber: number, unit: Unit, grossStrokes: number) {
    const url = `/orgs/${organizationId}/matches/${matchId}/hole-scores`
    const body = {
      team_side: unit.side,
      match_participant_id: unit.matchParticipantId,
      hole_number: holeNumber,
      gross_strokes: grossStrokes,
    }
    await enqueueWrite({ url, method: "POST", body, matchId })
    setPendingStrokes((prev) => new Map(prev).set(strokeKey(holeNumber, unit), grossStrokes))
    setPendingCount((c) => c + 1)
  }

  async function queueHoleResult(holeNumber: number, winningSide: "a" | "b" | null) {
    const url = `/orgs/${organizationId}/matches/${matchId}/hole-results`
    const body = { hole_number: holeNumber, winning_side: winningSide }
    await enqueueWrite({ url, method: "POST", body, matchId })
    setPendingResults((prev) => new Map(prev).set(holeNumber, winningSide))
    setPendingCount((c) => c + 1)
  }

  async function refetchScorecard() {
    const res = await fetch(`/orgs/${organizationId}/matches/${matchId}/scorecard`, {
      credentials: "include",
    })
    if (res.ok) setScorecard(await res.json())
  }

  useEffect(() => {
    let cancelled = false
    async function load() {
      try {
        const [sessionsRes, teamsRes, matchesRes] = await Promise.all([
          fetch(`/orgs/${organizationId}/tournaments/${tournamentId}/sessions`, { credentials: "include" }),
          fetch(`/orgs/${organizationId}/tournaments/${tournamentId}/teams`, { credentials: "include" }),
          fetch(`/orgs/${organizationId}/sessions/${sessionId}/matches`, { credentials: "include" }),
        ])
        if (!sessionsRes.ok || !teamsRes.ok || !matchesRes.ok) throw new Error("load failed")
        const sessionsData: ApiSession[] = await sessionsRes.json()
        const foundSession = sessionsData.find((s) => s.id === sessionId)
        if (!foundSession) throw new Error("session not found")
        const teamsData: ApiTeam[] = await teamsRes.json()
        if (teamsData.length !== 2) throw new Error("expected 2 teams")
        const matchesData: ApiMatch[] = await matchesRes.json()
        const foundMatch = matchesData.find((m) => m.id === matchId)
        if (!foundMatch) throw new Error("match not found")

        const [holesRes, scorecardRes] = await Promise.all([
          fetch(`/orgs/${organizationId}/courses/${foundSession.course_id}/holes`, { credentials: "include" }),
          fetch(`/orgs/${organizationId}/matches/${matchId}/scorecard`, { credentials: "include" }),
        ])
        if (!holesRes.ok || !scorecardRes.ok) throw new Error("load failed")
        const holesData: ApiHole[] = await holesRes.json()
        const scorecardData: ApiScorecard = await scorecardRes.json()

        if (cancelled) return
        setSession(foundSession)
        setTeams([teamsData[0], teamsData[1]])
        setMatch(foundMatch)
        setHoles(holesData)
        setScorecard(scorecardData)
        const played = playedHoleNumbers(foundSession.hole_config)
        const nextUnregistered = played[scorecardData.holes.length] ?? played[played.length - 1]
        setCurrentHole(nextUnregistered)

        const count = await queueCount(matchId).catch(() => 0)
        if (!cancelled) setPendingCount(count)
        if (count > 0 && navigator.onLine) void flushPending()
      } catch {
        if (!cancelled) setError("Klarte ikke å laste scorekortet. Prøv å laste siden på nytt.")
      } finally {
        if (!cancelled) setLoading(false)
      }
    }
    void load()
    return () => {
      cancelled = true
    }
  }, [organizationId, tournamentId, sessionId, matchId])

  const units: Unit[] = useMemo(() => {
    if (!session || !match || !teams) return []
    const isIndividual = INDIVIDUAL_FORMATS.includes(session.format)
    if (isIndividual) {
      return match.participants.map((p) => ({
        id: p.id,
        side: p.team_side,
        label: p.player_name,
        matchParticipantId: p.id,
      }))
    }
    return [
      { id: "a", side: "a", label: teams[0].name, matchParticipantId: null },
      { id: "b", side: "b", label: teams[1].name, matchParticipantId: null },
    ]
  }, [session, match, teams])

  const decided = scorecard?.points_side_a !== null && scorecard?.points_side_b !== null

  async function submitStroke(holeNumber: number, unit: Unit, grossStrokes: number) {
    setError(null)
    if (!navigator.onLine) {
      await queueStroke(holeNumber, unit, grossStrokes)
      return
    }
    let res: Response
    try {
      res = await fetch(`/orgs/${organizationId}/matches/${matchId}/hole-scores`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        credentials: "include",
        body: JSON.stringify({
          team_side: unit.side,
          match_participant_id: unit.matchParticipantId,
          hole_number: holeNumber,
          gross_strokes: grossStrokes,
        }),
      })
    } catch {
      // Ekte nettverksfeil (ikke bare et avvist svar) -- køordne i stedet
      // for å vise en feil, se ADR-028.
      await queueStroke(holeNumber, unit, grossStrokes)
      return
    }
    if (!res.ok) {
      setError("Klarte ikke å registrere slaget. Kanskje matchen allerede er avgjort.")
      return
    }
    await refetchScorecard()
  }

  // Walkover/konsesjon (ADR-024): kun kaptein for laget som GIR SEG (eller
  // org-admin) får lov -- backend håndhever dette, UI-et viser bare begge
  // knappene og lar en 403 melde seg som en vanlig feiltekst, samme mønster
  // som resten av appen (ingen klientside-forhåndsfiltrering på kapteinstatus).
  async function concede(teamId: string) {
    setError(null)
    try {
      const res = await fetch(`/orgs/${organizationId}/matches/${matchId}/concede`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        credentials: "include",
        body: JSON.stringify({ conceding_team_id: teamId }),
      })
      if (!res.ok) {
        const body = await res.json().catch(() => null)
        throw new Error(body?.detail?.message ?? "Klarte ikke å registrere walkover.")
      }
      setConcedeTeamIndex(null)
      setShowConcede(false)
      await refetchScorecard()
    } catch (e) {
      setError(e instanceof Error ? e.message : "Klarte ikke å registrere walkover.")
    }
  }

  async function submitHoleResult(holeNumber: number, winningSide: "a" | "b" | null) {
    setError(null)
    if (!navigator.onLine) {
      await queueHoleResult(holeNumber, winningSide)
      return
    }
    let res: Response
    try {
      res = await fetch(`/orgs/${organizationId}/matches/${matchId}/hole-results`, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        credentials: "include",
        body: JSON.stringify({ hole_number: holeNumber, winning_side: winningSide }),
      })
    } catch {
      await queueHoleResult(holeNumber, winningSide)
      return
    }
    if (!res.ok) {
      setError("Klarte ikke å registrere hull-resultatet. Kanskje matchen allerede er avgjort.")
      return
    }
    await refetchScorecard()
  }

  const programHref = `/tournaments/${tournamentId}/program?org=${organizationId}&name=${encodeURIComponent(tournamentName)}`

  if (loading || !session || !teams || !match || !scorecard || currentHole === null) {
    return (
      <div className="flex min-h-[100dvh] flex-col bg-background">
        <header className="sticky top-0 z-10 border-b border-border bg-background/80 backdrop-blur">
          <div className="mx-auto flex w-full max-w-3xl items-center gap-3 px-5 py-4">
            <Link
              href={programHref}
              aria-label="Tilbake til program"
              className="flex size-10 shrink-0 items-center justify-center rounded-xl border border-border bg-card text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
            >
              <ArrowLeft aria-hidden="true" className="size-5" />
            </Link>
            <div className="flex min-w-0 flex-col">
              <span className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
                Turnering
              </span>
              <h1 className="truncate text-xl font-extrabold tracking-tight text-foreground">
                {tournamentName}
              </h1>
            </div>
          </div>
        </header>
        <main className="mx-auto flex w-full max-w-3xl flex-1 items-center justify-center px-5 py-6">
          {error ? (
            <p role="alert" className="text-sm font-medium text-destructive">
              {error}
            </p>
          ) : (
            <div
              aria-hidden="true"
              className="size-10 animate-spin rounded-full border-4 border-primary/20 border-t-primary"
            />
          )}
        </main>
      </div>
    )
  }

  const played = playedHoleNumbers(session.hole_config)
  const totalHoles = played.length
  const par = holes.find((h) => h.hole_number === currentHole)?.par ?? 4
  const registeredHoleNumbers = new Set([
    ...scorecard.holes.map((h) => h.hole_number),
    ...Array.from(pendingStrokes.keys(), (k) => Number(k.split(":")[0])),
    ...Array.from(pendingResults.keys()),
  ])

  const statusLabel = scorecard.status_text ?? "AS"

  function strokeValue(holeNumber: number, unit: Unit): number | null {
    const key = strokeKey(holeNumber, unit)
    if (pendingStrokes.has(key)) return pendingStrokes.get(key)!
    const entry = (scorecard!.stroke_entries ?? []).find(
      (e) =>
        e.hole_number === holeNumber &&
        e.team_side === unit.side &&
        e.match_participant_id === unit.matchParticipantId,
    )
    return entry ? entry.gross_strokes : null
  }

  const currentResultEntry = pendingResults.has(currentHole)
    ? { hole_number: currentHole, winning_side: pendingResults.get(currentHole) ?? null }
    : (scorecard.hole_result_entries ?? []).find((e) => e.hole_number === currentHole)

  function goPrev() {
    setCurrentHole((h) => {
      const idx = played.indexOf(h!)
      return played[Math.max(0, idx - 1)]
    })
  }
  function goNext() {
    setCurrentHole((h) => {
      const idx = played.indexOf(h!)
      return played[Math.min(played.length - 1, idx + 1)]
    })
  }

  const isIndividual = INDIVIDUAL_FORMATS.includes(session.format)
  const isLast = played.indexOf(currentHole) >= played.length - 1

  return (
    <div className="flex min-h-[100dvh] flex-col bg-background">
      <header className="sticky top-0 z-20 border-b border-border bg-background/80 backdrop-blur">
        <div className="mx-auto flex w-full max-w-3xl items-center gap-3 px-5 py-4">
          <Link
            href={programHref}
            aria-label="Tilbake til program"
            className="flex size-10 shrink-0 items-center justify-center rounded-xl border border-border bg-card text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
          >
            <ArrowLeft aria-hidden="true" className="size-5" />
          </Link>
          <div className="flex min-w-0 flex-col">
            <span className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">
              Turnering
            </span>
            <h1 className="truncate text-xl font-extrabold tracking-tight text-foreground">
              {tournamentName}
            </h1>
            <span className="truncate text-sm text-muted-foreground">
              {session.name?.trim() || "Økt"} · {FORMAT_LABELS[session.format]} · Match {match.sequence}
            </span>
          </div>
        </div>

        <div className="border-t border-border bg-card/40">
          <div className="mx-auto grid w-full max-w-3xl grid-cols-[1fr_auto_1fr] items-center gap-2 px-5 py-3 sm:gap-4">
            <TeamTag name={teams[0].name} color={teams[0].color ?? "#64748b"} align="left" />
            {decided ? (
              <div className="flex flex-col items-center px-1">
                <span className="text-lg font-extrabold tracking-tight text-primary">Avgjort</span>
              </div>
            ) : (
              <div className="flex items-center gap-1.5 sm:gap-3">
                <button
                  type="button"
                  onClick={goPrev}
                  disabled={played.indexOf(currentHole) <= 0}
                  aria-label="Forrige hull"
                  className="flex size-11 shrink-0 items-center justify-center rounded-2xl border border-border bg-card text-foreground transition-colors hover:bg-accent/50 disabled:opacity-40"
                >
                  <ChevronLeft aria-hidden="true" className="size-5" />
                </button>
                <div className="flex min-w-[84px] flex-col items-center">
                  <span className="text-2xl font-extrabold leading-none tracking-tight text-foreground tabular-nums">
                    {statusLabel}
                  </span>
                </div>
                <button
                  type="button"
                  onClick={goNext}
                  disabled={isLast}
                  aria-label="Neste hull"
                  className="flex size-11 shrink-0 items-center justify-center rounded-2xl border border-border bg-card text-foreground transition-colors hover:bg-accent/50 disabled:opacity-40"
                >
                  <ChevronRight aria-hidden="true" className="size-5" />
                </button>
              </div>
            )}
            <TeamTag name={teams[1].name} color={teams[1].color ?? "#64748b"} align="right" />
          </div>
        </div>
      </header>

      <main className="mx-auto w-full max-w-3xl flex-1 px-5 py-6 sm:py-8">
        {!isOnline && (
          <div className="mb-4 flex items-center gap-2 rounded-2xl border border-amber-300 bg-amber-50 px-4 py-3 text-sm font-medium text-amber-900 dark:border-amber-900 dark:bg-amber-950 dark:text-amber-200">
            <WifiOff aria-hidden="true" className="size-4 shrink-0" />
            <span>Du er offline. Endringer lagres lokalt og sendes automatisk når du er tilbake på nett.</span>
          </div>
        )}
        {pendingCount > 0 && (
          <div className="mb-4 flex items-center justify-between gap-2 rounded-2xl border border-border bg-card px-4 py-3 text-sm font-medium text-foreground">
            <span>
              {pendingCount} {pendingCount === 1 ? "endring venter" : "endringer venter"} på synkronisering.
            </span>
            {isOnline && (
              <button
                type="button"
                onClick={() => void flushPending()}
                disabled={syncing}
                className="inline-flex shrink-0 items-center gap-1 font-semibold text-primary disabled:opacity-50"
              >
                <RefreshCw aria-hidden="true" className={cn("size-3.5", syncing && "animate-spin")} />
                Synkroniser nå
              </button>
            )}
          </div>
        )}
        {error && (
          <p role="alert" className="mb-4 text-sm font-medium text-destructive">
            {error}
          </p>
        )}

        {decided ? (
          <DecidedView
            teams={teams}
            scorecard={scorecard}
            holes={holes}
            units={units}
            scoringMode={session.scoring_mode}
          />
        ) : (
          <>
            <nav aria-label="Velg hull" className="-mx-5 mb-4 overflow-x-auto px-5">
              <div className="flex gap-2 pb-1">
                {played.map((h) => {
                  const active = h === currentHole
                  const registered = registeredHoleNumbers.has(h)
                  return (
                    <button
                      key={h}
                      type="button"
                      onClick={() => setCurrentHole(h)}
                      aria-current={active ? "true" : undefined}
                      className={cn(
                        "relative flex size-11 shrink-0 flex-col items-center justify-center rounded-2xl border text-sm font-bold tabular-nums transition-colors",
                        active
                          ? "border-primary bg-primary text-primary-foreground"
                          : "border-border bg-card text-foreground hover:bg-accent/50",
                      )}
                    >
                      {h}
                      {registered && (
                        <span
                          aria-hidden="true"
                          className={cn(
                            "absolute -right-0.5 -top-0.5 flex size-4 items-center justify-center rounded-full border-2 border-background",
                            active ? "bg-primary-foreground text-primary" : "bg-primary text-primary-foreground",
                          )}
                        >
                          <Check className="size-2.5" strokeWidth={3} />
                        </span>
                      )}
                    </button>
                  )
                })}
              </div>
            </nav>

            <div className="mb-4 overflow-hidden rounded-2xl border border-border bg-card">
              <button
                type="button"
                onClick={() => setShowSummary((v) => !v)}
                aria-expanded={showSummary}
                className="flex w-full items-center justify-between gap-2 px-4 py-3 text-left"
              >
                <span className="text-sm font-semibold text-foreground">Vis full oversikt</span>
                <ChevronsUpDown aria-hidden="true" className="size-4 text-muted-foreground" />
              </button>
              {showSummary && (
                <div className="border-t border-border">
                  <HoleSummaryTable teams={teams} scorecard={scorecard} holes={holes} scoringMode={session.scoring_mode} />
                </div>
              )}
            </div>

            <div className="mb-6 overflow-hidden rounded-2xl border border-border bg-card">
              <button
                type="button"
                onClick={() => {
                  setShowConcede((v) => !v)
                  setConcedeTeamIndex(null)
                }}
                aria-expanded={showConcede}
                className="flex w-full items-center justify-between gap-2 px-4 py-3 text-left"
              >
                <span className="text-sm font-semibold text-foreground">Gi opp matchen (walkover)</span>
                <ChevronsUpDown aria-hidden="true" className="size-4 text-muted-foreground" />
              </button>
              {showConcede && (
                <div className="flex flex-col gap-3 border-t border-border p-4">
                  <p className="text-xs leading-relaxed text-muted-foreground text-pretty">
                    Kun kapteinen for laget som gir seg (eller en organisasjonsadministrator) kan
                    gjøre dette. Motstanderen får full poengsum for matchen, uansett hvor mange hull
                    som allerede er registrert.
                  </p>
                  {teams.map((team, i) =>
                    concedeTeamIndex === i ? (
                      <div
                        key={team.id}
                        className="flex flex-col gap-3 rounded-xl bg-destructive/5 p-3 sm:flex-row sm:items-center sm:justify-between"
                      >
                        <span className="text-sm text-foreground text-pretty">
                          Er du sikker på at <span className="font-semibold">{team.name}</span> gir seg?
                        </span>
                        <div className="flex shrink-0 gap-2">
                          <Button
                            type="button"
                            size="sm"
                            variant="destructive"
                            className="rounded-xl"
                            onClick={() => concede(team.id)}
                          >
                            Gi opp
                          </Button>
                          <Button
                            type="button"
                            size="sm"
                            variant="ghost"
                            className="rounded-xl"
                            onClick={() => setConcedeTeamIndex(null)}
                          >
                            Avbryt
                          </Button>
                        </div>
                      </div>
                    ) : (
                      <Button
                        key={team.id}
                        type="button"
                        variant="outline"
                        className="h-11 justify-start rounded-xl text-sm font-semibold"
                        onClick={() => setConcedeTeamIndex(i as 0 | 1)}
                      >
                        <span aria-hidden="true" className="mr-2 size-3 rounded-full" style={{ backgroundColor: team.color ?? "#64748b" }} />
                        {team.name} gir seg
                      </Button>
                    ),
                  )}
                </div>
              )}
            </div>

            {session.scoring_mode === "stroke" ? (
              <section aria-label={`Hull ${currentHole}`}>
                <div className="mb-4 flex items-baseline gap-2">
                  <h2 className="text-2xl font-extrabold tracking-tight text-foreground">Hull {currentHole}</h2>
                  <span className="text-lg font-bold text-muted-foreground">· Par {par}</span>
                </div>
                <div className="flex flex-col gap-3">
                  {units.map((unit) => {
                    const teamColor = (unit.side === "a" ? teams[0].color : teams[1].color) ?? "#64748b"
                    const value = strokeValue(currentHole, unit)
                    const isPending = pendingStrokes.has(strokeKey(currentHole, unit))
                    return (
                      <div
                        key={unit.id}
                        className="flex flex-col gap-3 rounded-3xl border border-border bg-card p-4 shadow-sm shadow-black/5 sm:p-5"
                        style={{ borderLeftWidth: 6, borderLeftColor: teamColor }}
                      >
                        <div className="flex items-center gap-2.5">
                          <span aria-hidden="true" className="size-3.5 shrink-0 rounded-full" style={{ backgroundColor: teamColor }} />
                          <span className="text-base font-bold tracking-tight text-foreground text-pretty">
                            {unit.label}
                          </span>
                          {!isIndividual && (
                            <span className="text-xs font-medium text-muted-foreground">(delt ball)</span>
                          )}
                        </div>
                        <StrokePicker
                          value={value}
                          par={par}
                          isPending={isPending}
                          onSelect={(n) => void submitStroke(currentHole, unit, n)}
                        />
                      </div>
                    )
                  })}
                </div>
              </section>
            ) : (
              <section aria-label={`Hull ${currentHole}`}>
                <div className="mb-4 flex items-baseline gap-2">
                  <h2 className="text-2xl font-extrabold tracking-tight text-foreground">Hull {currentHole}</h2>
                  <span className="text-lg font-bold text-muted-foreground">· Par {par}</span>
                </div>
                <div className="grid grid-cols-1 gap-3 sm:grid-cols-3">
                  {(
                    [
                      { key: "a", label: `${teams[0].name} vant`, color: teams[0].color, side: "a" as const },
                      { key: "h", label: "Delt", color: undefined, side: null },
                      { key: "b", label: `${teams[1].name} vant`, color: teams[1].color, side: "b" as const },
                    ] as const
                  ).map((opt) => {
                    const active = currentResultEntry
                      ? currentResultEntry.winning_side === opt.side
                      : false
                    return (
                      <button
                        key={opt.key}
                        type="button"
                        onClick={() => submitHoleResult(currentHole, opt.side)}
                        aria-pressed={active}
                        className={cn(
                          "flex h-16 items-center justify-center gap-2 rounded-2xl border text-base font-bold tracking-tight transition-colors",
                          active
                            ? opt.color
                              ? "border-transparent text-primary-foreground"
                              : "border-primary bg-primary text-primary-foreground"
                            : "border-border bg-card text-foreground hover:bg-accent/50",
                        )}
                        style={active && opt.color ? { backgroundColor: opt.color } : undefined}
                      >
                        {opt.color && (
                          <span
                            aria-hidden="true"
                            className="size-3.5 rounded-full"
                            style={{ backgroundColor: active ? undefined : opt.color }}
                          />
                        )}
                        {opt.label}
                      </button>
                    )
                  })}
                </div>
                {pendingResults.has(currentHole) && (
                  <p className="mt-2 text-xs font-medium text-muted-foreground">
                    Lagret lokalt · venter på synk
                  </p>
                )}
              </section>
            )}

            <div className="mt-6">
              {!isLast ? (
                <Button type="button" onClick={goNext} className="h-14 w-full rounded-2xl text-base font-bold shadow-sm">
                  Neste hull
                  <ChevronRight aria-hidden="true" className="size-5" />
                </Button>
              ) : (
                <Button
                  type="button"
                  onClick={() => setShowSummary(true)}
                  className="h-14 w-full rounded-2xl text-base font-bold shadow-sm"
                >
                  <Check aria-hidden="true" className="size-5" />
                  Ferdig
                </Button>
              )}
            </div>
          </>
        )}
      </main>
    </div>
  )
}

// --- Small components --------------------------------------------------------

function TeamTag({ name, color, align }: { name: string; color: string; align: "left" | "right" }) {
  const right = align === "right"
  return (
    <div className={cn("flex min-w-0 items-center gap-2", right ? "flex-row-reverse text-right" : "text-left")}>
      <span aria-hidden="true" className="size-3.5 shrink-0 rounded-full" style={{ backgroundColor: color }} />
      <span className="truncate text-sm font-bold tracking-tight text-foreground">{name}</span>
    </div>
  )
}

// Tallvelger for slagregistrering: 1-9 direkte, pluss et "10 eller flere"-
// valg som åpner en ny rad med 10-19. Erstatter en tidligere pluss/minus-
// stepper -- raskere å treffe riktig tall banebruk (uten å måtte klikke seg
// opp/ned ett og ett slag).
function StrokePicker({
  value,
  par,
  isPending,
  onSelect,
}: {
  value: number | null
  par: number
  isPending: boolean
  onSelect: (n: number) => void
}) {
  const [showHigh, setShowHigh] = useState(value !== null && value >= 10)

  useEffect(() => {
    setShowHigh(value !== null && value >= 10)
  }, [value])

  const numbers = showHigh
    ? Array.from({ length: 10 }, (_, i) => i + 10)
    : Array.from({ length: 9 }, (_, i) => i + 1)

  return (
    <div className="flex flex-col gap-2">
      <div className="grid grid-cols-5 gap-2">
        {numbers.map((n) => (
          <button
            key={n}
            type="button"
            onClick={() => onSelect(n)}
            aria-pressed={value === n}
            className={cn(
              "flex h-12 items-center justify-center rounded-xl border text-base font-bold tabular-nums transition-colors active:scale-95",
              value === n
                ? "border-primary bg-primary text-primary-foreground"
                : "border-border bg-card text-foreground hover:bg-accent/50",
            )}
          >
            {n}
          </button>
        ))}
        {!showHigh && (
          <button
            type="button"
            onClick={() => setShowHigh(true)}
            className="flex h-12 items-center justify-center rounded-xl border border-dashed border-border bg-card text-xs font-bold text-muted-foreground transition-colors hover:bg-accent/50"
          >
            10+
          </button>
        )}
      </div>
      <div className="flex items-center justify-between gap-2">
        {showHigh ? (
          <button
            type="button"
            onClick={() => setShowHigh(false)}
            className="text-xs font-semibold text-primary"
          >
            ← Tilbake til 1–9
          </button>
        ) : (
          <span />
        )}
        <span className="text-xs font-medium text-muted-foreground">
          {value === null
            ? "Ikke registrert"
            : isPending
              ? "Lagret lokalt · venter på synk"
              : scoreToParLabel(value, par)}
        </span>
      </div>
    </div>
  )
}

function HoleSummaryTable({
  teams,
  scorecard,
  holes,
  scoringMode,
}: {
  teams: [ApiTeam, ApiTeam]
  scorecard: ApiScorecard
  holes: ApiHole[]
  scoringMode: ScoringMode
}) {
  if (scorecard.holes.length === 0) {
    return <p className="px-4 py-4 text-sm text-muted-foreground">Ingen hull er registrert ennå.</p>
  }

  return (
    <ul className="divide-y divide-border">
      {scorecard.holes.map((h) => {
        const par = holes.find((x) => x.hole_number === h.hole_number)?.par
        return (
          <li key={h.hole_number} className="flex items-center justify-between gap-3 px-4 py-2.5">
            <div className="flex items-center gap-2.5">
              <span className="flex size-8 items-center justify-center rounded-xl bg-secondary text-sm font-extrabold tabular-nums text-secondary-foreground">
                {h.hole_number}
              </span>
              {par !== undefined && (
                <span className="text-xs font-medium text-muted-foreground">Par {par}</span>
              )}
              {scoringMode === "stroke" && (
                <span className="text-xs tabular-nums text-muted-foreground">
                  {grossPairLabel(scorecard, h.hole_number)}
                </span>
              )}
            </div>
            <OutcomeBadge teams={teams} result={h.result} />
          </li>
        )
      })}
    </ul>
  )
}

function grossPairLabel(scorecard: ApiScorecard, holeNumber: number): string {
  const entries = scorecard.stroke_entries ?? []
  const sideGross = (side: "a" | "b"): string => {
    const vals = entries.filter((e) => e.hole_number === holeNumber && e.team_side === side).map((e) => e.gross_strokes)
    if (vals.length === 0) return "–"
    return String(Math.min(...vals))
  }
  return `${sideGross("a")}–${sideGross("b")}`
}

function OutcomeBadge({ teams, result }: { teams: [ApiTeam, ApiTeam]; result: "a" | "b" | "halved" }) {
  if (result === "halved") {
    return (
      <span className="inline-flex items-center rounded-full bg-secondary px-2.5 py-1 text-xs font-semibold text-secondary-foreground">
        Delt
      </span>
    )
  }
  const team = result === "a" ? teams[0] : teams[1]
  return (
    <span
      className="inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-xs font-semibold text-primary-foreground"
      style={{ backgroundColor: team.color ?? "#64748b" }}
    >
      {team.name}
    </span>
  )
}

function scoreToParLabel(value: number, par: number): string {
  const diff = value - par
  if (diff === 0) return "Par"
  if (diff === -1) return "Birdie"
  if (diff === -2) return "Eagle"
  if (diff <= -3) return "Albatross"
  if (diff === 1) return "Bogey"
  if (diff === 2) return "Dobbel bogey"
  return `+${diff}`
}

function DecidedView({
  teams,
  scorecard,
  holes,
  units,
  scoringMode,
}: {
  teams: [ApiTeam, ApiTeam]
  scorecard: ApiScorecard
  holes: ApiHole[]
  units: Unit[]
  scoringMode: ScoringMode
}) {
  return (
    <div className="flex flex-col gap-6">
      <div className="flex items-center gap-4 rounded-3xl bg-primary px-5 py-5 text-primary-foreground shadow-sm">
        <div className="flex size-14 shrink-0 items-center justify-center rounded-2xl bg-primary-foreground/15">
          <Trophy aria-hidden="true" className="size-7" />
        </div>
        <div className="flex flex-col">
          <span className="text-lg font-extrabold tracking-tight">Matchen er avgjort</span>
          <span className="text-sm text-primary-foreground/85 text-pretty">{scorecard.status_text ?? ""}</span>
        </div>
      </div>

      <div className="grid grid-cols-3 items-center gap-2 rounded-3xl border border-border bg-card px-5 py-4 shadow-sm">
        <div className="flex flex-col items-start gap-1">
          <div className="flex items-center gap-2">
            <span aria-hidden="true" className="size-3.5 rounded-full" style={{ backgroundColor: teams[0].color ?? "#64748b" }} />
            <span className="text-sm font-bold text-foreground text-pretty">{teams[0].name}</span>
          </div>
          <span className="text-3xl font-extrabold tabular-nums text-foreground">
            {formatPoints(scorecard.points_side_a ?? 0)}
          </span>
        </div>
        <div className="text-center text-xs font-semibold uppercase tracking-wide text-muted-foreground">Poeng</div>
        <div className="flex flex-col items-end gap-1">
          <div className="flex flex-row-reverse items-center gap-2">
            <span aria-hidden="true" className="size-3.5 rounded-full" style={{ backgroundColor: teams[1].color ?? "#64748b" }} />
            <span className="text-sm font-bold text-foreground text-pretty">{teams[1].name}</span>
          </div>
          <span className="text-3xl font-extrabold tabular-nums text-foreground">
            {formatPoints(scorecard.points_side_b ?? 0)}
          </span>
        </div>
      </div>

      <div>
        <h2 className="mb-3 text-sm font-semibold uppercase tracking-wide text-muted-foreground">Hull for hull</h2>
        <div className="overflow-hidden rounded-2xl border border-border bg-card">
          <HoleSummaryTable teams={teams} scorecard={scorecard} holes={holes} scoringMode={scoringMode} />
        </div>
        <p className="mt-3 text-center text-xs text-muted-foreground text-pretty">
          Resultatet kan ikke endres etter at matchen er avgjort.
        </p>
      </div>
    </div>
  )
}

function formatPoints(points: number) {
  return Number.isInteger(points) ? String(points) : points.toFixed(1).replace(".", ",")
}
