#!/usr/bin/env python3
"""lane_page.py — one page shell for every review page this lane writes (film3 item 12).

Eleven pages per film, sixteen writers each with their own CSS, the only shared piece a
menu injected afterwards — and the portal (preview/review/run) already had design
tokens none of them used. Every page now opens the same way: the portal's tokens, one
header that names the ONE question the page answers, the same verdict pill, the same
clock (film time beside song time once a delivery's cards.json exists). The writers keep
their bodies; they stop carrying a doctype and a palette of their own.

    from lane_page import page, pill, clock
    doc = page("w04 — the first take", "Is this face on speed, before anything else renders?",
               body_html, slug=slug, extra_css=".row{display:flex}")

TOKENS is the portal's `:root` block verbatim (src/video/preview-portal/shared-assets.ts);
a test keeps the two equal, so the lane and the portal never drift apart in colour.
"""
from __future__ import annotations

import html
import os as _os
import sys as _sys

_sys.path.insert(0, _os.path.dirname(_os.path.abspath(__file__)))
import raplib  # noqa: E402

TOKENS = ':root{color-scheme:dark;--bg:#0a0a0d;--panel:#14141a;--panel2:#1a1a22;--line:#232330;--ink:#e8e6e1;--ink2:#b6b6c0;--ink3:#7a7a86;--accent:#ffb454;--good:#5fc792;--danger:#d8556a;--glow:rgba(255,180,84,.06);--font-body:-apple-system,BlinkMacSystemFont,"Segoe UI",Inter,sans-serif;--font-display:var(--font-body);--font-wordmark:var(--font-display)}'

BASE_CSS = """
*{box-sizing:border-box}
body{background:var(--bg);color:var(--ink);font:14px/1.55 var(--font-body);margin:0;padding:24px;max-width:1240px}
a{color:var(--accent);text-decoration:none}a:hover{text-decoration:underline}
header.hero{margin:0 0 22px}.eyebrow{font-size:11px;font-weight:600;letter-spacing:.22em;text-transform:uppercase;color:var(--ink3)}
h1{font-size:22px;font-weight:800;letter-spacing:-.01em;line-height:1.15;margin:6px 0 6px}
p.sub{color:var(--ink2);max-width:92ch;margin:0}
h2{font-size:15px;margin:22px 0 8px;color:var(--ink2)}h3{font-size:13px;margin:16px 0 6px;color:var(--ink2)}
p{max-width:100ch}
table{border-collapse:collapse;width:100%}th,td{padding:7px 10px;border-bottom:1px solid var(--line);vertical-align:top;text-align:left}
th{color:var(--ink3);font-weight:600;font-size:11px;letter-spacing:.08em;text-transform:uppercase}
td.num{font-variant-numeric:tabular-nums;text-align:right}
code{font-family:ui-monospace,"SF Mono",monospace;font-size:12.5px;background:var(--panel);padding:1px 6px;border-radius:4px}
pre{font-family:ui-monospace,"SF Mono",monospace;background:var(--panel);border:1px solid var(--line);border-radius:8px;padding:12px;white-space:pre-wrap;font-size:12.5px}
.muted,.meta{color:var(--ink3)}
.ok,.pass{color:var(--good);font-weight:700}.bad,.fail{color:var(--danger);font-weight:700}.warn,.advisory{color:var(--accent);font-weight:700}
.pill{display:inline-block;padding:2px 9px;border-radius:999px;font-size:11px;font-weight:700;letter-spacing:.06em;text-transform:uppercase;border:1px solid var(--line);background:var(--panel);color:var(--ink2)}
.pill.pass{color:var(--good);background:rgba(95,199,146,.14)}.pill.fail{color:var(--danger);background:rgba(216,85,106,.16)}
.pill.advisory{color:var(--accent);background:rgba(255,180,84,.14)}.pill.skip{color:var(--ink3)}
.clock{font-family:ui-monospace,"SF Mono",monospace;font-size:12px;color:var(--ink2);white-space:nowrap}.clock small{color:var(--ink3)}
.strip{overflow-x:auto}.strip img{max-width:none;border:1px solid var(--line);border-radius:6px;display:block}
video,img.frame{border-radius:6px;border:1px solid var(--line)}audio{width:260px;height:32px}
.card,.panel{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:14px}
"""

VERDICTS = ("PASS", "ADVISORY", "FAIL", "SKIP")


def pill(verdict: str, text: str | None = None) -> str:
    """One palette for every gate result on every page: PASS / ADVISORY / FAIL / SKIP."""
    v = (verdict or "").upper()
    if v not in VERDICTS:
        v = "ADVISORY"
    return f'<span class="pill {v.lower()}">{html.escape(text if text is not None else v)}</span>'


def front_padding_for(project_dir: str) -> float | None:
    """Seconds of bookend before the body on the last delivery, or None before one."""
    cards = _os.path.join(project_dir, "build", "bookends", "cards.json")
    if not _os.path.isfile(cards):
        return None
    try:
        return raplib.front_padding(cards)
    except Exception:  # noqa: BLE001 — a bad manifest is not this page's problem
        return None


def film_clock(song_s: float, front_pad: float | None) -> str:
    """"1:48.3" on the film's clock (what the operator reads in QuickTime), or "" before a delivery."""
    if front_pad is None:
        return ""
    t = float(song_s) + float(front_pad)
    return f"{int(t // 60)}:{t % 60:04.1f}"


def clock(song_s: float, front_pad: float | None = None) -> str:
    """The one clock: film time first when it exists, song seconds always."""
    fc = film_clock(song_s, front_pad)
    if fc:
        return f'<span class="clock">{fc}<small> · song {float(song_s):.1f}s</small></span>'
    return f'<span class="clock">{float(song_s):.2f}s<small> song</small></span>'


def page(title: str, question: str, body: str, *, slug: str = "", extra_css: str = "",
         eyebrow: str = "rap-mv") -> str:
    """The whole document: doctype, the portal's tokens, the base rules, the writer's own
    rules, then one header — eyebrow · title · the question this page answers — and the
    body. The lane menu is added by lane_nav after the file exists."""
    eb = f"{eyebrow} · {slug}" if slug else eyebrow
    return (
        '<!doctype html><html lang="en"><head><meta charset="utf-8">'
        '<meta name="viewport" content="width=device-width,initial-scale=1">'
        f"<title>{html.escape(title)}</title>"
        f"<style>{TOKENS}{BASE_CSS}{extra_css}</style></head><body>"
        f'<header class="hero"><div class="eyebrow">{html.escape(eb)}</div>'
        f"<h1>{title}</h1>"
        f'<p class="sub">{question}</p></header>'
        f"{body}</body></html>"
    )
