#!/usr/bin/env python3
"""Approved logo system sheet: Late Sun mark (primary), compact cut, Eight-Ray Aster
(secondary, round crops), and the mark-above-name arrangement. The name is live
type set in PhotoCraft, never part of the logo artwork.

  python build_logo_system.py <wordmark_source.psd> <out_basename>
"""
import json
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import build_logos as BL  # noqa: E402

W, H = 3600, 2500
INK, CREAM, VIOLET, GOLD, BROWN = BL.INK, BL.CREAM, BL.VIOLET, BL.GOLD, BL.BROWN
LIB_DISPLAY_CAP = 0.658  # cap height / em for Linux Libertine Display G


def build(psd, out):
    L = BL.Logos(psd)
    body = [f'<rect id="bg" width="{W}" height="{H}" fill="{CREAM}"/>']
    T = []  # type steps

    def tile(id_, x, y, w, h, fill="#EAE0CD"):
        body.append(f'<rect id="{id_}" x="{x}" y="{y}" width="{w}" height="{h}" fill="{fill}"/>')

    def label(x, y, text, name, size=22, color="#6B4733", **kw):
        T.append({"id": "type.create", "params": {"x": x, "y": y, "text": text, "name": name, "font": "Inter",
                                                  "fontStyle": "Medium", "size": size, "tracking": 160, "color": color, **kw}})

    # row 1: the mark, three ways
    r = 210
    for i, (cw, fill, colr, eye, line, mono) in enumerate([
            ("color", "#EAE0CD", VIOLET, GOLD, INK, False),
            ("mono", "#EAE0CD", INK, GOLD, INK, True),
            ("reversed", BROWN, CREAM, GOLD, CREAM, True)]):
        x = 150 + i * 1125
        tile(f"tile-mark-{cw}", x, 420, 1050, 640, fill)
        body += L.late_sun_paths(x + 525, 420 + 400, r, color=colr, eye=eye, line=line, mono=mono, pid=f"mark-{cw}")
        label(x + 40, 420 + 600, {"color": "FULL COLOR", "mono": "ONE COLOR", "reversed": "REVERSED"}[cw], f"cap-mark-{cw}",
              color="#6B4733" if cw != "reversed" else CREAM)

    # row 2: above the name (Late Sun = primary, Eight-Ray = alternate)
    y2 = 1160
    for i, (key, title) in enumerate([("late-sun", "PRIMARY · LATE SUN ABOVE THE NAME"), ("eight-ray", "ALTERNATE · EIGHT-RAY ASTER ABOVE THE NAME")]):
        x = 150 + i * 1690
        tile(f"tile-lockup-{key}", x, y2, 1610, 700)
        cap = 92
        size = round(cap / LIB_DISPLAY_CAP, 1)
        name_w = L.wm.width * cap
        cx = x + 805
        base = y2 + 520
        if key == "late-sun":
            rr = 0.27 * name_w
            body += L.late_sun_paths(cx, base - cap - 0.55 * cap - 0.105 * rr, rr, pid="lockup-late-sun")
        else:
            rr = 0.15 * name_w
            body += L.symbol_paths(cx, base - cap - 0.55 * cap - rr, rr, pid="lockup-eight-ray")
        T.append({"id": "type.create", "params": {"x": cx, "y": base, "text": "Aster Chai", "name": f"name-{key}", "align": "center",
                                                  "font": "Linux Libertine Display G", "size": size, "color": INK}})
        label(x + 40, y2 + 660, title, f"cap-lockup-{key}")

    # row 3: compact cut, secondary mark in a round crop, small sizes
    y3 = 1960
    tile("tile-compact", 150, y3, 1050, 440)
    body += BL.late_sun_compact_paths(L, 150 + 300, y3 + 250, 150, pid="compact-large")
    body += BL.late_sun_compact_paths(L, 150 + 640, y3 + 236, 48, pid="compact-48")
    body += BL.late_sun_compact_paths(L, 150 + 770, y3 + 233, 16, pid="compact-32")
    label(190, y3 + 410, "COMPACT CUT · 48 PX AND BELOW", "cap-compact")
    tile("tile-avatar", 1275, y3, 1050, 440)
    body.append(f'<circle id="avatar-disc" cx="{1275 + 525}" cy="{y3 + 210}" r="165" fill="{BROWN}"/>')
    body += L.symbol_paths(1275 + 525, y3 + 210, 120, color="#C9BFD9", eye=GOLD, pid="avatar-eight-ray")
    label(1315, y3 + 410, "SECONDARY · EIGHT-RAY ASTER FOR ROUND CROPS", "cap-avatar")
    tile("tile-16px", 2400, y3, 1050, 440)
    label(2440, y3 + 410, "16 PX · 8× AND ACTUAL SIZE", "cap-16px")

    with open(out + ".svg", "w", encoding="utf-8") as fh:
        fh.write(BL.svg_doc(W, H, "\n".join(body), "Aster Chai logo system"))
    T += [
        {"id": "type.create", "params": {"x": 150, "y": 250, "text": "Logo · Late Sun", "name": "title",
                                         "font": "Linux Libertine Display G", "size": 110, "color": INK}},
        {"id": "type.create", "params": {"x": 150, "y": 322, "text": "The mark rises over the line. The name is set beneath it, never inside it.",
                                         "name": "subtitle", "font": "Linux Libertine G", "fontStyle": "Italic", "size": 44, "color": "#6B4733"}},
    ]
    json.dump(T, open(out + ".recipe.json", "w", encoding="utf-8"))
    return T


if __name__ == "__main__":
    t = build(sys.argv[1], sys.argv[2])
    print(json.dumps(t, separators=(",", ":")))
