from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
R = Path("raw"); O = Path("out")
F = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; FB = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
def font(s, b=False): return ImageFont.truetype(FB if b else F, s)
NAMES = {"demo-ato-alpha": "Full school (24 reviews)", "demo-ato-charlie": "Empty, unclaimed school", "demo-ato-bravo": "School with 2 reviews (under 3)"}
DEV = {390: "iPhone (390 px)", 1440: "Desktop (1440 px)"}
CHANGES = {
 "demo-ato-alpha": "AFTER: score and review count shown once (top card) · one certificate count · 'Pilot' kept in course names · replies open, own school only · courses aligned, one count · contact row in the top card on phones · clearer grey text",
 "demo-ato-charlie": "AFTER: clear 'Is this your school? Claim this page' step in the top card · Call / Email / Website shown disabled · no dead empty stars or zero bars · no placeholder sentence",
 "demo-ato-bravo": "AFTER: no per-review stars and no bars until 3 reviews · '2 of 3 reviews needed for a rating' instead of a self-contradicting line",
}
def pair(slug, w, theme, scale=1.0, out=None):
    a = Image.open(R / f"before-{slug}-{w}-{theme}.png").convert("RGB")
    b = Image.open(R / f"after-{slug}-{w}-{theme}.png").convert("RGB")
    k = (0.5 if w < 700 else 1.0) * scale          # mobile shots are 2x
    a = a.resize((int(a.width * k), int(a.height * k)), Image.LANCZOS); b = b.resize((int(b.width * k), int(b.height * k)), Image.LANCZOS)
    pad, gap = int(40 * max(scale, .6)), int(40 * max(scale, .6))
    head, lab = int(150 * max(scale, .6)), int(56 * max(scale, .6))
    W = a.width + b.width + gap + 2 * pad
    sub = CHANGES[slug]
    fs = font(int(22 * max(scale, .6)))
    # wrap the change line
    lines, cur = [], ""
    for word in sub.split(" "):
        t = (cur + " " + word).strip()
        if fs.getlength(t) > W - 2 * pad: lines.append(cur); cur = word
        else: cur = t
    lines.append(cur)
    lh = int(30 * max(scale, .6))
    ft = font(int(32 * max(scale, .6)), True); th = int(42 * max(scale, .6))
    tl, cur = [], ""
    for word in f"{NAMES[slug]} · {DEV[w]} · {theme.capitalize()}".split(" "):
        t = (cur + " " + word).strip()
        if ft.getlength(t) > W - 2 * pad: tl.append(cur); cur = word
        else: cur = t
    tl.append(cur)
    top0 = int(20 * max(scale, .6)) + th * len(tl) + 6
    head = top0 + lh * len(lines) + 16
    H = head + lab + max(a.height, b.height) + pad
    im = Image.new("RGB", (W, H), (236, 236, 240)); d = ImageDraw.Draw(im)
    for i, l in enumerate(tl): d.text((pad, int(20 * max(scale, .6)) + i * th), l, font=ft, fill=(17, 17, 20))
    for i, l in enumerate(lines): d.text((pad, top0 + i * lh), l, font=fs, fill=(60, 60, 66))
    for x, img, t, col in ((pad, a, "BEFORE (today's generator)", (120, 120, 128)), (pad + a.width + gap, b, "AFTER (suggested)", (20, 110, 60))):
        d.text((x, head + 8), t, font=font(int(26 * max(scale, .6)), True), fill=col)
        im.paste(img, (x, head + lab)); d.rectangle((x - 1, head + lab - 1, x + img.width, head + lab + img.height), outline=(190, 190, 198))
    if out: im.save(out, optimize=True); print(out, im.size)
    return im
names = []
for slug in ("demo-ato-alpha", "demo-ato-charlie"):
    for w in (390, 1440):
        for t in ("midnight", "pearl"):
            short = "full" if slug.endswith("alpha") else "empty"
            pair(slug, w, t, scale=2.0 if w < 700 else 1.0, out=O / f"pair-{short}-school-{w}-{t}.png")
pair("demo-ato-bravo", 390, "midnight", scale=2.0, out=O / "extra-pair-two-reviews-390-midnight.png")
# member form extra
a = Image.open(R / "before-member-form-390-midnight.png").convert("RGB"); b = Image.open(R / "after-member-form-390-midnight.png").convert("RGB")
im = Image.new("RGB", (a.width + b.width + 120, a.height + 170), (236, 236, 240)); d = ImageDraw.Draw(im)
d.text((40, 20), "Write-a-review form (members) · iPhone · Midnight", font=font(32, True), fill=(17, 17, 20))
d.text((40, 66), "AFTER: every star button is a 44 x 44 px tap target (was 18 px wide); stars get their own wider column", font=font(22), fill=(60, 60, 66))
d.text((40, 112), "BEFORE (today's generator)", font=font(26, True), fill=(120, 120, 128)); d.text((80 + a.width, 112), "AFTER (suggested)", font=font(26, True), fill=(20, 110, 60))
im.paste(a, (40, 160)); im.paste(b, (80 + a.width, 160)); im.save(O / "extra-pair-review-form-stars-390-midnight.png", optimize=True)

# contact sheet: best 4 pairs
picks = [("demo-ato-alpha", 390, "midnight", 1.0), ("demo-ato-charlie", 390, "pearl", 1.0),
         ("demo-ato-alpha", 1440, "pearl", .55), ("demo-ato-charlie", 1440, "midnight", .55)]
tiles = [pair(s, w, t, scale=sc) for s, w, t, sc in picks]
row1 = tiles[:2]; row2 = tiles[2:]
W = max(sum(t.width for t in row1), sum(t.width for t in row2)) + 60
H1 = max(t.height for t in row1); H2 = max(t.height for t in row2)
top = 130
sheet = Image.new("RGB", (W, top + H1 + H2 + 60), (250, 250, 252)); d = ImageDraw.Draw(sheet)
d.text((30, 24), "Demo ATO pages: BEFORE (today's generator) vs AFTER (suggested fixes)", font=font(40, True), fill=(17, 17, 20))
d.text((30, 80), "Made-up Demo ATO data only. Mockup, not committed or live. Left of each pair = before, right = after.", font=font(24), fill=(80, 80, 88))
x = 30
for t in row1: sheet.paste(t, (x, top)); x += t.width + 0
x = 30
for t in row2: sheet.paste(t, (x, top + H1 + 30)); x += t.width + 0
sheet.save(O / "CONTACT-SHEET-ato-before-after.png", optimize=True); print("sheet", sheet.size)
