#!/usr/bin/env bash
# Render a committed trutina scorecard (report.html) to a trimmed PNG figure.
#
#   render-scorecard.sh <report.html> <out.png>
#
# Headless Chrome defaults prefers-color-scheme to light, so this re-asserts the
# dark lab palette before shooting, then top-crops the blank tail (if Pillow is
# present). Reproduces docs/notes/img/scorecard-4family.png from
# examples/support-ticket/sample-4family/report.html.
set -euo pipefail
SRC="${1:?usage: render-scorecard.sh <report.html> <out.png>}"
OUT="${2:?usage: render-scorecard.sh <report.html> <out.png>}"

CHROME="${CHROME:-}"
if [ -z "$CHROME" ]; then
  for c in \
    "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \
    "/Applications/Chromium.app/Contents/MacOS/Chromium" \
    "$(command -v google-chrome || true)" \
    "$(command -v chromium || true)"; do
    [ -n "$c" ] && [ -x "$c" ] && CHROME="$c" && break
  done
fi
[ -n "$CHROME" ] || { echo "no Chrome/Chromium found; set CHROME=/path/to/chrome" >&2; exit 1; }

TMP="$(mktemp -t scorecard).html"
trap 'rm -f "$TMP"' EXIT
python3 - "$SRC" "$TMP" <<'PY'
import sys
src = open(sys.argv[1], encoding="utf-8").read()
dark = ('<style id="force-dark">:root{'
        '--bg:#0e1014;--bg-2:#14171d;--panel:#14171d;--ink:#e7e9ee;--ink-dim:#aeb6c2;'
        '--muted:#79828f;--line:#232831;--line-strong:#313845;--accent:#58c4dd;'
        '--pass:#5fd29a;--pass-bg:#11271d;--warn:#e8c06a;--warn-bg:#2a2310;'
        '--fail:#ec7e95;--fail-bg:#2c1218;}</style></head>')
open(sys.argv[2], "w", encoding="utf-8").write(src.replace("</head>", dark, 1))
PY

"$CHROME" --headless --disable-gpu --hide-scrollbars --force-device-scale-factor=2 \
  --screenshot="$OUT" --window-size=1000,2200 "file://$TMP" >/dev/null 2>&1

# Top-crop the blank tail. No-op (full frame kept) if Pillow is unavailable.
python3 - "$OUT" <<'PY' || true
import sys
try:
    from PIL import Image
except ImportError:
    sys.exit(0)
im = Image.open(sys.argv[1]).convert("RGB"); W, H = im.size; px = im.load()
bg = (14, 16, 20)
def blank(y):
    return all(abs(px[x, y][0] - bg[0]) + abs(px[x, y][1] - bg[1]) + abs(px[x, y][2] - bg[2]) <= 18
               for x in range(0, W, 7))
last = next((y for y in range(H - 1, -1, -1) if not blank(y)), H - 1)
im.crop((0, 0, W, min(H, last + 1 + 48))).save(sys.argv[1])
PY
echo "wrote $OUT"
