#!/usr/bin/env python3
"""Generate the bundled, base64-embedded card fonts for the agent first post.

Instantiates static Regular (400) and Medium (500) weights from the ABC Monument
Grotesk *variable* font and writes them, base64-encoded, into a generated
TypeScript module. The renderer (Satori) needs static TTF/OTF — it can't read
woff2 and is unreliable with variable fonts — so we pin the two weights the
brand template uses and embed them inline (same approach as the greeting cards,
so the compiled bun binary and the published Node CLI both ship the bytes with
no external asset files).

Run from the package root:  python3 scripts/gen-card-fonts.py
Source font: cli-docs/docs/public/fonts/ABCMonumentGroteskVariableVF.woff
"""

import base64
import io
import os

from fontTools import ttLib
from fontTools.varLib.instancer import instantiateVariableFont

HERE = os.path.dirname(os.path.abspath(__file__))
PKG_ROOT = os.path.dirname(HERE)
REPO_ROOT = os.path.dirname(os.path.dirname(PKG_ROOT))
SRC_FONT = os.path.join(
    REPO_ROOT, "cli-docs", "docs", "public", "fonts", "ABCMonumentGroteskVariableVF.woff"
)
OUT_TS = os.path.join(PKG_ROOT, "src", "lib", "agent", "card-fonts.ts")

# The two weights the brand meme template uses: caption (Medium) + handle (Regular).
WEIGHTS = {"regular": 400, "medium": 500}


def instantiate_ttf_base64(weight: int) -> str:
    font = ttLib.TTFont(SRC_FONT)
    # Pin the variable axes to a single static instance (upright, target weight).
    instantiateVariableFont(font, {"wght": weight, "slnt": 0}, inplace=True)
    font.flavor = None  # emit a bare TTF (drop woff compression)
    buf = io.BytesIO()
    font.save(buf)
    return base64.b64encode(buf.getvalue()).decode("ascii")


def main() -> None:
    fonts = {name: instantiate_ttf_base64(wght) for name, wght in WEIGHTS.items()}

    lines = [
        "// AUTO-GENERATED by scripts/gen-card-fonts.py — do not edit by hand.",
        "// Static Regular (400) + Medium (500) instances of ABC Monument Grotesk,",
        "// instantiated from the variable font and inlined as base64 TTF so the",
        "// card renderer ships the bytes in both the bun binary and the Node CLI.",
        "/* eslint-disable */",
        "",
        "import { Buffer } from \"node:buffer\";",
        "",
    ]
    for name in WEIGHTS:
        lines.append(f'const {name}Base64 =')
        lines.append(f'  "{fonts[name]}";')
        lines.append("")
    lines.append("/** ABC Monument Grotesk Regular (400) as raw TTF bytes. */")
    lines.append('export const MONUMENT_REGULAR = Buffer.from(regularBase64, "base64");')
    lines.append("/** ABC Monument Grotesk Medium (500) as raw TTF bytes. */")
    lines.append('export const MONUMENT_MEDIUM = Buffer.from(mediumBase64, "base64");')
    lines.append("")

    with open(OUT_TS, "w") as f:
        f.write("\n".join(lines))

    sizes = ", ".join(f"{n}={len(b) * 3 // 4 // 1024}KB" for n, b in fonts.items())
    print(f"Wrote {OUT_TS} ({sizes})")


if __name__ == "__main__":
    main()
