"""Passlabb pl ligature: a lowercase p and l joined into one shape (pass + labb), the l in gold.
Writes app/public/brand/ (SVG mark, lockup, favicons) and the email logos in app/public/email/.
Run: python3 tools/brand/make.py   (needs Playwright's Chromium and the Inter font)"""
import os, asyncio
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "app", "public")
OUT, MAIL = os.path.join(ROOT, "brand"), os.path.join(ROOT, "email")

INK = "#1E1A14"; GOLD_D = "#9A7432"
def mark(uid="pl"):
    return f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs>
<linearGradient id="{uid}p" gradientUnits="userSpaceOnUse" x1="20" y1="28" x2="60" y2="96"><stop offset="0" stop-color="#C9A24A"/><stop offset="1" stop-color="#8A6A2F"/></linearGradient>
<linearGradient id="{uid}l" gradientUnits="userSpaceOnUse" x1="66" y1="6" x2="96" y2="96"><stop offset="0" stop-color="#F3DE9E"/><stop offset=".5" stop-color="#D4AF4F"/><stop offset="1" stop-color="#A8822E"/></linearGradient></defs>
<path d="M25 34V93" fill="none" stroke="url(#{uid}p)" stroke-width="13" stroke-linecap="round"/>
<circle cx="45" cy="52" r="20" fill="none" stroke="url(#{uid}p)" stroke-width="13"/>
<path d="M76 9V70Q76 88 93 88" fill="none" stroke="url(#{uid}l)" stroke-width="13" stroke-linecap="round"/></svg>'''

def lockup(tag=True):
    return f'''<div style="display:inline-flex;align-items:center;gap:14px;padding:6px;font-family:Inter,sans-serif">{mark("lk")[:4]} width="64" height="64"{mark("lk")[4:]}
<div style="display:flex;flex-direction:column;gap:7px"><div style="font:800 46px/1 Inter;letter-spacing:-.045em;color:{INK}">pass<span style="background:linear-gradient(100deg,#B8893A,#E6C77E 55%,#B8893A);-webkit-background-clip:text;color:transparent">labb</span></div>
{f'<div style="font:600 11px/1 Inter;letter-spacing:.42em;color:{GOLD_D};text-transform:uppercase">Luxury trip design</div>' if tag else ''}</div></div>'''

async def main():
    os.makedirs(OUT, exist_ok=True)
    open(os.path.join(OUT, "pl-mark.svg"), "w").write(mark())
    open(os.path.join(OUT, "favicon.svg"), "w").write(mark("fv"))
    from playwright.async_api import async_playwright
    async with async_playwright() as p:
        b = await p.chromium.launch(); pg = await b.new_page(device_scale_factor=1)
        async def shot(html, path, w, h, transparent=True):
            await pg.set_viewport_size({"width": w, "height": h})
            await pg.set_content(f'<html><body style="margin:0;background:transparent"><div id="x" style="width:{w}px;height:{h}px;display:grid;place-items:center">{html}</div></body></html>')
            await pg.wait_for_timeout(150)
            await pg.locator("#x").screenshot(path=path, omit_background=transparent)
        m = lambda s: mark("s")[:4] + f' width="{s}" height="{s}"' + mark("s")[4:]
        await shot(m(32), os.path.join(OUT, "favicon-32.png"), 32, 32)
        await shot(f'<div style="width:180px;height:180px;background:#FFFFFF;display:grid;place-items:center">{m(132)}</div>', os.path.join(OUT, "apple-touch-icon.png"), 180, 180, False)
        await shot(f'<div style="width:512px;height:512px;background:#FFFFFF;display:grid;place-items:center">{m(380)}</div>', os.path.join(OUT, "pl-512.png"), 512, 512, False)
        await shot(m(68), os.path.join(MAIL, "logo-mark.png"), 68, 68)
        # email header logo: 375 x 84 (shown at 188 x 42)
        await pg.set_viewport_size({"width": 375, "height": 84})
        await pg.set_content(f'''<html><body style="margin:0;background:transparent"><div id="x" style="width:375px;height:84px;display:flex;align-items:center;gap:14px;font-family:Inter">{m(68)}
<div style="display:flex;flex-direction:column;gap:7px"><div style="font:800 46px/1 Inter;letter-spacing:-.045em;color:{INK}">pass<span style="background:linear-gradient(100deg,#B8893A,#E6C77E 55%,#B8893A);-webkit-background-clip:text;color:transparent">labb</span></div>
<div style="font:600 11px/1 Inter;letter-spacing:.42em;color:{GOLD_D};text-transform:uppercase">Luxury trip design</div></div></div></body></html>''')
        await pg.wait_for_timeout(150); await pg.locator("#x").screenshot(path=os.path.join(MAIL, "logo.png"), omit_background=True)
        await pg.set_viewport_size({"width": 900, "height": 300})
        await pg.set_content(f'<html><body style="margin:0;background:#fff"><div id="x" style="padding:30px;display:inline-block">{lockup()}</div></body></html>')
        await pg.wait_for_timeout(150); await pg.locator("#x").screenshot(path=os.path.join(OUT, "passlabb-logo.png"))
        await b.close()
    print("brand files written")
asyncio.run(main())
