"""SolarPlants brand PNG asset generator.

All PNGs are transparent-background. Two color systems:

  · POSITIVE — dark element (Ink + Copper) — for use on LIGHT backgrounds.
  · NEGATIVE — light element (Bone + Copper) — for use on DARK backgrounds.

Plus two single-color modes (no copper accent — for fax, legal, photocopy):

  · POSITIVE MONO — Ink only — on light bg
  · NEGATIVE MONO — Bone only — on dark bg

Outputs in examples/brand-assets/logos/png/:

  Wordmark "SolarPlants + Twin Peaks Mark":
    · wordmark-positive-{400,800,1600}.png       (Ink + copper)
    · wordmark-negative-{400,800,1600}.png       (Bone + copper)
    · wordmark-mono-positive-{400,800,1600}.png  (Ink only)
    · wordmark-mono-negative-{400,800,1600}.png  (Bone only)

  S-Pictogram (capital "S" + Twin Peaks top-right):
    · s-pictogram-positive-{200,400,800,1600}.png       (Ink + copper)
    · s-pictogram-negative-{200,400,800,1600}.png       (Bone + copper)
    · s-pictogram-mono-positive-{200,400,800,1600}.png  (Ink only)
    · s-pictogram-mono-negative-{200,400,800,1600}.png  (Bone only)

All transparent backgrounds. Use the variant whose element color contrasts with the surface.
"""
from PIL import Image, ImageDraw, ImageFont
from pathlib import Path

ROOT = Path("/Users/ordonic/Desktop/AGENTS/Solarplants/examples/brand-assets")
OUT = ROOT / "logos" / "png"
OUT.mkdir(parents=True, exist_ok=True)

# Font — PIL can't read Fraunces OTF (CFF), use Georgia as render fallback.
# Canonical brand font remains Fraunces in SVG/HTML/DOCX/PPTX which render via OS / Google Fonts.
FONT_PATH = "/System/Library/Fonts/Supplemental/Georgia.ttf"

# Brand tokens (RGBA)
INK = (14, 26, 36, 255)
COPPER = (184, 115, 51, 255)
BONE = (244, 241, 236, 255)
TRANSPARENT = (0, 0, 0, 0)


def draw_twin_peaks(draw, x_origin, y_origin, width, height, color, rear_opacity=0.55):
    """Draw Twin Peaks chevrons inside a width×height box at (x_origin, y_origin).
    Coordinates in 100×100 viewBox space, mapped to box size.
      Rear chevron: M 9,100 L 35,38 L 61,100  (opacity 55%)
      Front chevron: M 27,100 L 53,18 L 79,100
    """
    def pt(x, y):
        return (int(x_origin + x / 100 * width),
                int(y_origin + y / 100 * height))

    sw = max(2, int(min(width, height) * 0.10))
    rear = color[:3] + (int(255 * rear_opacity),)
    front = color[:3] + (255,)

    # Rear chevron (drawn first, behind)
    draw.line([pt(9, 100), pt(35, 38)], fill=rear, width=sw, joint="curve")
    draw.line([pt(35, 38), pt(61, 100)], fill=rear, width=sw, joint="curve")
    # Front chevron
    draw.line([pt(27, 100), pt(53, 18)], fill=front, width=sw, joint="curve")
    draw.line([pt(53, 18), pt(79, 100)], fill=front, width=sw, joint="curve")

    # Round end caps
    cap_r = sw // 2
    for x, y in [(9, 100), (35, 38), (61, 100)]:
        cx, cy = pt(x, y)
        draw.ellipse([cx - cap_r, cy - cap_r, cx + cap_r, cy + cap_r], fill=rear)
    for x, y in [(27, 100), (53, 18), (79, 100)]:
        cx, cy = pt(x, y)
        draw.ellipse([cx - cap_r, cy - cap_r, cx + cap_r, cy + cap_r], fill=front)


def render_wordmark(width_px, text_color, mark_color, mark_rear_opacity=0.55,
                    text="SolarPlants", padding_ratio=0.05):
    """Render wordmark + Twin Peaks Mark on transparent background.
    Baseline-aligned, punctuation-spaced. anchor='ls' for explicit baseline control.
    """
    scale = 4
    W = width_px * scale

    pad = int(W * padding_ratio)
    avail_width = W - 2 * pad
    font_size = int(avail_width / 5.2)
    font = ImageFont.truetype(FONT_PATH, font_size)

    text_w = int(font.getlength(text))
    ascent, descent = font.getmetrics()

    # Twin Peaks: ~28% of font_size tall — punctuation-sized
    mark_h = int(font_size * 0.28)
    mark_w = int(mark_h * 50 / 32)
    gap = int(font_size * 0.10)

    total_w = pad + text_w + gap + mark_w + pad
    total_h = pad + ascent + descent + pad

    # Always transparent bg
    canvas = Image.new("RGBA", (total_w, total_h), TRANSPARENT)
    draw = ImageDraw.Draw(canvas)

    baseline_y = pad + ascent
    text_x = pad
    draw.text((text_x, baseline_y), text, font=font, fill=text_color, anchor="ls")

    mark_x = text_x + text_w + gap
    mark_y_top = baseline_y - mark_h
    draw_twin_peaks(draw, mark_x, mark_y_top, mark_w, mark_h, mark_color, mark_rear_opacity)

    final_w = width_px
    final_h = int(total_h * final_w / total_w)
    return canvas.resize((final_w, final_h), Image.LANCZOS)


def render_s_pictogram(size, fg_color, mark_color, mark_rear_opacity=0.55):
    """Render 'S' + Twin Peaks top-right pictogram on transparent background.
    Capital S optically centered. Twin Peaks accent in top-right corner with breathing space.
    """
    scale = 4
    W = size * scale

    canvas = Image.new("RGBA", (W, W), TRANSPARENT)
    draw = ImageDraw.Draw(canvas)

    # The "S" — large, optically centered
    font_size = int(W * 0.78)
    font = ImageFont.truetype(FONT_PATH, font_size)

    # Use anchor='mm' (middle-middle) for clean centering
    cx, cy = W // 2, W // 2 + int(W * 0.03)  # slight bias down for optical balance
    draw.text((cx, cy), "S", font=font, fill=fg_color, anchor="mm")

    # Twin Peaks top-right — clear of S with margin
    mark_w = int(W * 0.24)
    mark_h = int(mark_w * 32 / 50)
    margin = int(W * 0.09)
    mark_x = W - margin - mark_w
    mark_y = margin
    draw_twin_peaks(draw, mark_x, mark_y, mark_w, mark_h, mark_color, mark_rear_opacity)

    return canvas.resize((size, size), Image.LANCZOS)


# ============ Build matrix ============
print("=" * 72)
print("SolarPlants Brand PNG Asset Library v2.4")
print("All assets transparent-bg · positive/negative + mono variants")
print("=" * 72)

WORDMARK_SIZES = [400, 800, 1600]
PICTO_SIZES = [200, 400, 800, 1600]

wordmark_variants = [
    # (name, text_color, mark_color, rear_opacity, description)
    ("positive",       INK,    COPPER, 0.55, "Ink + copper · on LIGHT backgrounds"),
    ("negative",       BONE,   COPPER, 0.55, "Bone + copper · on DARK backgrounds"),
    ("mono-positive",  INK,    INK,    0.45, "Ink one-color · on LIGHT backgrounds (fax/legal)"),
    ("mono-negative",  BONE,   BONE,   0.45, "Bone one-color · on DARK backgrounds (photos)"),
]

print("\n→ Wordmark PNGs (transparent bg):")
for vname, text_c, mark_c, rear, desc in wordmark_variants:
    print(f"\n  {vname.upper():20s}  {desc}")
    for size in WORDMARK_SIZES:
        img = render_wordmark(size, text_c, mark_c, mark_rear_opacity=rear)
        path = OUT / f"wordmark-{vname}-{size}.png"
        img.save(path, "PNG")
        print(f"    ✓ wordmark-{vname}-{size}.png  ({img.size[0]}×{img.size[1]})")

print("\n→ S-Pictogram PNGs (transparent bg):")
for vname, text_c, mark_c, rear, desc in wordmark_variants:
    print(f"\n  {vname.upper():20s}  {desc}")
    for size in PICTO_SIZES:
        img = render_s_pictogram(size, text_c, mark_c, mark_rear_opacity=rear)
        path = OUT / f"s-pictogram-{vname}-{size}.png"
        img.save(path, "PNG")
        print(f"    ✓ s-pictogram-{vname}-{size}.png  ({size}×{size})")

print("\n" + "=" * 72)
total_files = len(wordmark_variants) * len(WORDMARK_SIZES) + len(wordmark_variants) * len(PICTO_SIZES)
print(f"Generated {total_files} PNG files in {OUT}")
print("=" * 72)
