#!/usr/bin/env python3
import os, sys, json, subprocess
sys.path.insert(0, "/tmp/bd")
os.environ.setdefault("BD_OUT", "/tmp/bd/out")
from gen import *  # noqa

P = "bobby-directory"
GL, GD = GROUND_LIGHT, GROUND_DARK

# ============================================================ 1. CORE MARKS
# 1.1 standalone beeldmerk, three geometries, all schemes, transparent
for style, tag in (("full", "beeldmerk"), ("simplified", "beeldmerk-simplified"),
                   ("micro", "beeldmerk-micro")):
    u = SYMBOL_UNITS[style]
    for scheme in ("color-light", "color-dark", "black", "white", "grayscale"):
        write_svg(f"logo/{P}-{tag}-{scheme}.svg",
                  svg(u, u, SYMBOL[style](PAL[scheme]), None),
                  f"standalone {tag}, {scheme}, transparent background")
    for scheme, field, fname in (("color-light", GL, "on-light"), ("color-dark", GD, "on-dark")):
        write_svg(f"logo/{P}-{tag}-{scheme}-{fname}.svg",
                  svg(u, u, SYMBOL[style](PAL[scheme]), field),
                  f"standalone {tag}, {scheme}, {fname} field")

# 1.2 lockups
for case in ("mixed", "upper"):
    for scheme in ("color-light", "color-dark", "black", "white", "grayscale"):
        write_svg(f"logo/{P}-horizontal-{case}-{scheme}.svg",
                  horizontal(scheme, case), f"horizontal lockup, {case} case, {scheme}")
        write_svg(f"logo/{P}-stacked-{case}-{scheme}.svg",
                  stacked(scheme, case), f"stacked lockup, {case} case, {scheme}")
        write_svg(f"logo/{P}-wordmark-{case}-{scheme}.svg",
                  wordmark_only(scheme, case), f"wordmark only, two line, {case} case, {scheme}")
        write_svg(f"logo/{P}-wordmark-inline-{case}-{scheme}.svg",
                  wordmark_only(scheme, case, inline=True),
                  f"wordmark only, single line, {case} case, {scheme}")
for scheme, field, fname in (("color-light", GL, "on-light"), ("color-dark", GD, "on-dark")):
    write_svg(f"logo/{P}-horizontal-mixed-{scheme}-{fname}.svg",
              horizontal(scheme, "mixed", bg=field), f"horizontal lockup on {fname} field")
    write_svg(f"logo/{P}-stacked-mixed-{scheme}-{fname}.svg",
              stacked(scheme, "mixed", bg=field), f"stacked lockup on {fname} field")

# 1.3 simplified lockup for constrained widths
for scheme in ("color-light", "color-dark", "black", "white"):
    write_svg(f"logo/{P}-horizontal-simplified-{scheme}.svg",
              horizontal(scheme, "mixed", style="simplified"),
              f"simplified horizontal lockup, {scheme}")

# 1.4 PNG exports of the core marks
for tag in ("beeldmerk", "beeldmerk-simplified"):
    for scheme in ("color-light", "color-dark", "black", "white", "grayscale"):
        for sz in (1024, 512, 256, 128, 64):
            png(f"logo/{P}-{tag}-{scheme}.svg", f"logo/png/{P}-{tag}-{scheme}-{sz}.png",
                width=sz, height=sz, note=f"{tag} {scheme} {sz}px transparent")
for sz in (16, 32, 48, 64, 128):
    for scheme in ("color-light", "color-dark", "black", "white", "grayscale"):
        png(f"logo/{P}-beeldmerk-micro-{scheme}.svg",
            f"logo/png/{P}-beeldmerk-micro-{scheme}-{sz}.png", width=sz, height=sz,
            note=f"pixel snapped micro beeldmerk {scheme} {sz}px")
for case in ("mixed", "upper"):
    for scheme in ("color-light", "color-dark", "black", "white", "grayscale"):
        for h in (256, 128, 64):
            png(f"logo/{P}-horizontal-{case}-{scheme}.svg",
                f"logo/png/{P}-horizontal-{case}-{scheme}-h{h}.png", height=h,
                note=f"horizontal lockup {case} {scheme} {h}px tall")
        for h in (512, 256):
            png(f"logo/{P}-stacked-{case}-{scheme}.svg",
                f"logo/png/{P}-stacked-{case}-{scheme}-h{h}.png", height=h,
                note=f"stacked lockup {case} {scheme} {h}px tall")
        for h in (128, 64):
            png(f"logo/{P}-wordmark-{case}-{scheme}.svg",
                f"logo/png/{P}-wordmark-{case}-{scheme}-h{h}.png", height=h,
                note=f"wordmark {case} {scheme} {h}px tall")

# ============================================================ 2. APP ICONS
ICON_SIZES = (1024, 512, 256, 192, 180, 128, 64, 48, 32, 16)
ICON_SETS = [
    ("full-color-light", "color-light", "full", GL, False),
    ("full-color-dark", "color-dark", "full", GD, False),
    ("simplified-light", "color-light", "simplified", GL, False),
    ("simplified-dark", "color-dark", "simplified", GD, False),
    ("monochrome-dark", "white", "full", "#101828", False),
    ("monochrome-light", "black", "full", GL, False),
    ("transparent", "color-light", "full", None, False),
    ("transparent-dark", "color-dark", "full", None, False),
    ("masksafe-light", "color-light", "full", GL, True),
    ("masksafe-dark", "color-dark", "full", GD, True),
]
for name, scheme, style, field, bleed in ICON_SETS:
    safe = 0.60 if bleed else 0.66
    write_svg(f"app-icons/{P}-app-icon-{name}.svg",
              icon(scheme, style, field, 1024, safe=safe, bleed=bleed),
              f"app icon master, {name}" + (", full bleed mask safe" if bleed else ""))
    for sz in ICON_SIZES:
        if sz <= 32 and style == "full":
            src = f"app-icons/{P}-app-icon-{name.replace('full-color', 'simplified').replace('monochrome-dark','monochrome-dark').replace('transparent','transparent')}.svg"
            src = src if os.path.exists(os.path.join(ROOT, src)) else f"app-icons/{P}-app-icon-{name}.svg"
        else:
            src = f"app-icons/{P}-app-icon-{name}.svg"
        png(src, f"app-icons/{P}-app-icon-{name}-{sz}.png", width=sz, height=sz,
            note=f"app icon {name} {sz}px")

# ============================================================ 3. FAVICONS
write_svg(f"favicons/{P}-favicon.svg",
          f'<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64" role="img">'
          f'<style>@media (prefers-color-scheme: dark){{.i{{fill:#F2F4F7}}.s{{stroke:#F2F4F7}}.a{{fill:#FFB066}}}}</style>'
          f'<path class="s" d="M26 12H15a3 3 0 0 0-3 3v34a3 3 0 0 0 3 3h11" fill="none" stroke="#101828" stroke-width="6" stroke-linecap="round"/>'
          f'<rect class="i" x="32" y="11.5" width="21" height="18.5" rx="5" fill="#101828"/>'
          f'<rect class="a" x="32" y="33.5" width="21" height="18.5" rx="5" fill="#C2440A"/></svg>',
          "primary favicon, transparent, adapts to dark scheme")
write_svg(f"favicons/{P}-favicon-micro-16.svg", svg(16, 16, symbol_micro(PAL["color-light"])),
          "dedicated pixel snapped 16 px favicon geometry")
write_svg(f"favicons/{P}-favicon-micro-16-dark.svg", svg(16, 16, symbol_micro(PAL["color-dark"])),
          "dedicated pixel snapped 16 px favicon geometry, dark scheme")
write_svg(f"favicons/{P}-pinned-tab.svg",
          svg(16, 16, symbol_micro(PAL["black"])).replace('fill="#101828"', 'fill="#000000"'),
          "Safari pinned tab, single path colour, monochrome")
png(f"favicons/{P}-favicon-micro-16.svg", f"favicons/{P}-favicon-16.png", 16, 16,
    "16 px favicon from dedicated micro geometry")
png(f"favicons/{P}-beeldmerk-simplified-src.svg" if False else f"logo/{P}-beeldmerk-simplified-color-light.svg",
    f"favicons/{P}-favicon-32.png", 32, 32, "32 px favicon from simplified geometry")
png(f"logo/{P}-beeldmerk-simplified-color-light.svg", f"favicons/{P}-favicon-48.png", 48, 48,
    "48 px favicon from simplified geometry")
png(f"app-icons/{P}-app-icon-full-color-light.svg", f"favicons/{P}-apple-touch-icon.png", 180, 180,
    "apple touch icon, 180 px, opaque field")
subprocess.run(["magick",
                os.path.join(ROOT, f"favicons/{P}-favicon-16.png"),
                os.path.join(ROOT, f"favicons/{P}-favicon-32.png"),
                os.path.join(ROOT, f"favicons/{P}-favicon-48.png"),
                os.path.join(ROOT, f"favicons/{P}-favicon.ico")], check=True)
INDEX.append({"file": f"favicons/{P}-favicon.ico", "type": "ico", "note": "multi resolution 16, 32, 48"})

# ============================================================ 4. INVOICE AND RECEIPT
inv_sets = [("black", "black", None), ("pure-black", "pure-black", None),
            ("white-on-dark", "white", GD), ("grayscale", "grayscale", None),
            ("full-color", "color-light", None)]
for name, scheme, field in inv_sets:
    write_svg(f"invoice/{P}-invoice-logo-{name}.svg",
              horizontal(scheme, "mixed", bg=field, sym=64),
              f"print safe horizontal invoice logo, {name}")
    write_svg(f"invoice/{P}-receipt-logo-{name}.svg",
              horizontal(scheme, "mixed", style="simplified", bg=field, sym=40),
              f"compact receipt logo, {name}")
    for h in (600, 300):
        png(f"invoice/{P}-invoice-logo-{name}.svg",
            f"invoice/{P}-invoice-logo-{name}-h{h}.png", height=h,
            note=f"invoice logo {name}, {h}px tall, print quality")
    png(f"invoice/{P}-receipt-logo-{name}.svg", f"invoice/{P}-receipt-logo-{name}-h160.png",
        height=160, note=f"receipt logo {name}, 160px tall")
# thermal receipt: pure black, narrow, no anti-aliased fills below 1px
write_svg(f"invoice/{P}-receipt-thermal-mono.svg",
          horizontal("pure-black", "mixed", style="simplified", sym=32),
          "thermal receipt mark, pure monochrome, minimum ink")
png(f"invoice/{P}-receipt-thermal-mono.svg", f"invoice/{P}-receipt-thermal-mono-w384.png",
    width=384, note="thermal receipt mark, 384 px wide, 58 mm printers")

# ============================================================ 5. EMAIL
email_sets = [("light", "color-light", GL), ("dark", "color-dark", GD),
              ("mono", "black", GL), ("transparent", "color-light", None)]
for name, scheme, field in email_sets:
    write_svg(f"email/{P}-email-header-{name}.svg", email_header(scheme, field),
              f"transactional email header, {name}")
    write_svg(f"email/{P}-email-compact-{name}.svg",
              horizontal(scheme, "mixed", style="simplified", bg=field, sym=36),
              f"compact email logo, {name}")
    for w in (600, 1200):
        png(f"email/{P}-email-header-{name}.svg", f"email/{P}-email-header-{name}-{w}.png",
            width=w, note=f"email header {name}, {w}px wide")
    for w in (200, 400):
        png(f"email/{P}-email-compact-{name}.svg", f"email/{P}-email-compact-{name}-{w}.png",
            width=w, note=f"compact email logo {name}, {w}px wide")

# ============================================================ 6. AVATARS
AV = (1024, 512, 400, 256, 128)
avatar_sets = [("square-light", "color-light", GL, False), ("square-dark", "color-dark", GD, False),
               ("square-mono-light", "black", GL, False), ("square-mono-dark", "white", GD, False),
               ("circle-light", "color-light", GL, True), ("circle-dark", "color-dark", GD, True),
               ("circle-mono-light", "black", GL, True), ("circle-mono-dark", "white", GD, True)]
for name, scheme, field, circ in avatar_sets:
    write_svg(f"avatars/{P}-avatar-{name}.svg", avatar(scheme, field, 1024, circ),
              f"avatar master, {name}" + (", circle safe" if circ else ""))
    for sz in AV:
        png(f"avatars/{P}-avatar-{name}.svg", f"avatars/{P}-avatar-{name}-{sz}.png", sz, sz,
            note=f"avatar {name} {sz}px")

# ============================================================ 7. SOCIAL
SOCIAL = [
    ("x-profile", 400, 400, "avatar", "dark"),
    ("x-cover", 1500, 500, "banner", "dark"),
    ("linkedin-profile", 400, 400, "avatar", "dark"),
    ("linkedin-company-cover", 1128, 191, "banner", "light"),
    ("facebook-profile", 400, 400, "avatar", "dark"),
    ("facebook-cover", 1640, 856, "banner", "light"),
    ("instagram-profile", 320, 320, "avatar", "dark"),
    ("youtube-profile", 800, 800, "avatar", "dark"),
    ("youtube-channel-cover", 2560, 1440, "banner", "dark"),
    ("github-org-avatar", 500, 500, "avatar", "dark"),
    ("open-graph", 1200, 630, "banner", "dark"),
    ("social-share", 1200, 675, "banner", "light"),
]
SUB = "STRUCTURED DIRECTORIES"
for name, w, h, kind, mode in SOCIAL:
    scheme = "color-dark" if mode == "dark" else "color-light"
    field = GD if mode == "dark" else GL
    if kind == "avatar":
        content = avatar(scheme, field, max(w, h), circle=False)
    else:
        cy = h / 2
        if name == "youtube-channel-cover":
            content = banner(w, h, scheme, field, SUB, sym_frac=0.11, cy=h / 2)
        elif name == "linkedin-company-cover":
            content = banner_h(w, h, scheme, field, sym_frac=0.46, cx=w * 0.34)
        elif name == "facebook-cover":
            content = banner(w, h, scheme, field, SUB, sym_frac=0.24, cy=h * 0.44)
        elif name == "x-cover":
            content = banner_h(w, h, scheme, field, sym_frac=0.40)
        else:
            content = banner(w, h, scheme, field, SUB, sym_frac=0.24, cy=cy)
    write_svg(f"social/{P}-{name}.svg", content, f"social master, {name}, {w}x{h}")
    png(f"social/{P}-{name}.svg", f"social/{P}-{name}.png", width=w, height=h,
        note=f"social export {name}, {w}x{h}")

# ============================================================ 8. EMBED IMAGES
EMBED = [("open-graph-default", "Structured directories with an API"),
         ("directory-preview", "DIRECTORY PREVIEW"),
         ("profile-preview", "PROFILE PREVIEW"),
         ("developer-api", "API AND MCP"),
         ("documentation", "DOCUMENTATION"),
         ("fallback", None)]
for name, sub in EMBED:
    mode_dark = name in ("open-graph-default", "developer-api", "fallback")
    scheme = "color-dark" if mode_dark else "color-light"
    field = GD if mode_dark else GL
    sub_txt = sub.upper() if sub else None
    write_svg(f"embed/{P}-embed-{name}.svg",
              banner(1200, 630, scheme, field, sub_txt, sym_frac=0.22),
              f"embed image, {name}, 1200x630")
    png(f"embed/{P}-embed-{name}.svg", f"embed/{P}-embed-{name}.png", 1200, 630,
        note=f"embed export {name}, 1200x630")

with open(os.path.join(ROOT, "asset-index.json"), "w") as f:
    json.dump(INDEX, f, indent=2)
print("assets:", len(INDEX))
