#!/usr/bin/env python3
"""
Generates the SVG artwork bundled with the starter templates.

Every image is a palette-matched composition (gradient, soft shapes, large
Bootstrap Icons glyph). No photos are hotlinked, so templates never break
and carry no third-party licensing; merchants replace art with their own
uploads in the editor.

Usage (development only; output is committed):
    python3 tools/generate_template_art.py <path-to-bootstrap-icons/icons>
Writes public/assets/templates/art/<template-slug>/<name>.svg
"""
import os
import random
import re
import sys

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
OUT = os.path.join(ROOT, "public", "assets", "templates", "art")

# slug: (primary, secondary, light, dark, icons)
SETS = {
    "common":      ("#0f766e", "#5eead4", "#ecfdf8", "#134e4a", ["image", "stars", "bag-heart", "lightbulb", "people", "gem", "cup-hot", "camera"]),
    "fashion":     ("#c2410c", "#fdba74", "#f7efe8", "#1c1917", ["bag-heart", "handbag", "eyeglasses", "watch", "tag", "scissors", "gem", "gift"]),
    "electronics": ("#2563eb", "#22d3ee", "#eef4ff", "#0f172a", ["laptop", "phone", "headphones", "smartwatch", "camera", "controller", "speaker", "tv"]),
    "furniture":   ("#7c5c3b", "#d4a373", "#f6f1ea", "#3b2a1a", ["lamp", "house-heart", "inboxes", "easel", "flower1", "brightness-high", "gift", "tag"]),
    "jewellery":   ("#8a6d3b", "#e7c98a", "#faf6ef", "#1c1a17", ["gem", "suit-diamond", "stars", "heart", "watch", "gift", "magic", "sun"]),
    "grocery":     ("#16a34a", "#f97316", "#effbf2", "#14532d", ["basket", "basket3", "egg", "cup-straw", "cart", "basket2", "flower1", "sun"]),
    "beauty":      ("#be185d", "#f9a8d4", "#fdf2f8", "#500724", ["droplet", "flower2", "stars", "magic", "brush", "heart", "gem", "sun"]),
    "restaurant":  ("#b45309", "#fcd34d", "#fbf6ee", "#1f1611", ["cup-hot", "egg-fried", "fire", "basket2", "cup-straw", "cake2", "egg", "heart"]),
    "business":    ("#0f766e", "#99f6e4", "#effaf8", "#0b3b37", ["briefcase", "graph-up-arrow", "people", "lightbulb", "chat-dots", "shield-check", "globe", "bar-chart"]),
    "corporate":   ("#1e3a8a", "#38bdf8", "#eef3fb", "#0b1736", ["building", "globe", "shield-check", "bar-chart", "people", "briefcase", "graph-up-arrow", "lightbulb"]),
    "portfolio":   ("#e11d48", "#fda4af", "#f6f6f7", "#111827", ["camera2", "palette", "brush", "vector-pen", "image", "easel", "lightbulb", "stars"]),
    "landing":     ("#7c3aed", "#f59e0b", "#f5f3ff", "#2e1065", ["rocket-takeoff", "lightning", "phone", "chat-dots", "stars", "graph-up-arrow", "shield-check", "people"]),
    "healthcare":  ("#0284c7", "#34d399", "#eff8fd", "#0c2f44", ["heart-pulse", "hospital", "capsule", "clipboard2-pulse", "person-fill", "shield-check", "people", "heart"]),
    "education":   ("#4338ca", "#facc15", "#f2f2fd", "#1e1b4b", ["mortarboard", "book", "pencil", "backpack", "lightbulb", "people", "globe", "stars"]),
}


def icon_paths(icons_dir, name):
    with open(os.path.join(icons_dir, name + ".svg")) as fh:
        svg = fh.read()
    return "".join(re.findall(r"<path[^>]*/>", svg))


def blobs(rnd, w, h, color, n=3, opacity=(0.12, 0.28)):
    out = []
    for _ in range(n):
        r = rnd.uniform(0.18, 0.42) * min(w, h)
        cx, cy = rnd.uniform(0, w), rnd.uniform(0, h)
        out.append(f'<circle cx="{cx:.0f}" cy="{cy:.0f}" r="{r:.0f}" fill="{color}" opacity="{rnd.uniform(*opacity):.2f}"/>')
    return "".join(out)


def svg(w, h, body, defs=""):
    return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {w} {h}" width="{w}" height="{h}" '
            f'preserveAspectRatio="xMidYMid slice"><defs>{defs}</defs>{body}</svg>')


def glyph(paths, cx, cy, size, fill, opacity=1.0):
    s = size / 16.0
    return (f'<g transform="translate({cx - size / 2:.1f} {cy - size / 2:.1f}) scale({s:.3f})" '
            f'fill="{fill}" opacity="{opacity}">{paths}</g>')


def scene(rnd, w, h, prim, sec, light, dark, paths, mode):
    gid = "g%d" % rnd.randint(1000, 9999)
    if mode == "dark":
        defs = f'<linearGradient id="{gid}" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="{dark}"/><stop offset="1" stop-color="{prim}"/></linearGradient>'
        body = f'<rect width="{w}" height="{h}" fill="url(#{gid})"/>' + blobs(rnd, w, h, sec, 4, (0.10, 0.22))
        body += glyph(paths, w * 0.72, h * 0.5, min(w, h) * 0.5, "#ffffff", 0.18)
    elif mode == "product":
        defs = f'<radialGradient id="{gid}" cx=".5" cy=".38" r=".75"><stop offset="0" stop-color="#ffffff"/><stop offset="1" stop-color="{light}"/></radialGradient>'
        body = f'<rect width="{w}" height="{h}" fill="url(#{gid})"/>' + blobs(rnd, w, h, sec, 2, (0.10, 0.18))
        body += f'<ellipse cx="{w / 2}" cy="{h * 0.78}" rx="{w * 0.26}" ry="{h * 0.035}" fill="{dark}" opacity=".10"/>'
        body += glyph(paths, w / 2, h * 0.47, min(w, h) * 0.46, prim, 0.92)
    elif mode == "person":
        defs = f'<linearGradient id="{gid}" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="{light}"/><stop offset="1" stop-color="{sec}"/></linearGradient>'
        body = f'<rect width="{w}" height="{h}" fill="url(#{gid})"/>' + blobs(rnd, w, h, "#ffffff", 2, (0.25, 0.4))
        body += glyph(paths, w / 2, h * 0.62, min(w, h) * 0.9, dark, 0.55)
    else:  # light scene
        defs = f'<linearGradient id="{gid}" x1="0" y1="0" x2="1" y2="1"><stop offset="0" stop-color="{light}"/><stop offset="1" stop-color="{sec}"/></linearGradient>'
        body = f'<rect width="{w}" height="{h}" fill="url(#{gid})"/>' + blobs(rnd, w, h, prim, 3, (0.08, 0.16))
        body += glyph(paths, w * 0.5, h * 0.5, min(w, h) * 0.42, prim, 0.85)
    return svg(w, h, body, defs)


def main(icons_dir):
    person = icon_paths(icons_dir, "person-fill")
    total = 0
    for slug, (prim, sec, light, dark, icons) in SETS.items():
        rnd = random.Random(slug)  # deterministic per template
        out = os.path.join(OUT, slug)
        os.makedirs(out, exist_ok=True)
        P = [icon_paths(icons_dir, i) for i in icons]
        files = {
            "hero.svg": scene(rnd, 1200, 960, prim, sec, light, dark, P[0], "light"),
            "banner.svg": scene(rnd, 1600, 700, prim, sec, light, dark, P[1], "dark"),
            "about.svg": scene(rnd, 1000, 800, prim, sec, light, dark, P[2], "light"),
            "cta.svg": scene(rnd, 1600, 600, prim, sec, light, dark, P[3], "dark"),
        }
        for i in range(8):
            files[f"p{i + 1}.svg"] = scene(rnd, 800, 800, prim, sec, light, dark, P[i % len(P)], "product")
        for i in range(4):
            files[f"c{i + 1}.svg"] = scene(rnd, 800, 1000, prim, sec, light, dark, P[(i + 2) % len(P)], "dark" if i % 2 else "light")
            files[f"t{i + 1}.svg"] = scene(rnd, 600, 600, prim, sec, light, dark, person, "person")
        for i in range(3):
            files[f"b{i + 1}.svg"] = scene(rnd, 800, 500, prim, sec, light, dark, P[(i + 4) % len(P)], "light" if i % 2 else "dark")
            files[f"a{i + 1}.svg"] = scene(rnd, 200, 200, prim, sec, light, dark, person, "person")
        for name, content in files.items():
            with open(os.path.join(out, name), "w") as fh:
                fh.write(content)
            total += 1
    print(f"wrote {total} files to {OUT}")


if __name__ == "__main__":
    if len(sys.argv) != 2:
        sys.exit(__doc__)
    main(sys.argv[1])
