#!/usr/bin/env python3
"""Kings & Queens — Poster Inscripción Clases Virtuales
   SECOND PASS: refined spacing, tighter typography, extra polish
"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H   = 1080, 1350
FONTS  = '/home/edd/Proyectos/Edd-OS/.claude/skills/canvas-design/canvas-fonts/'
OUTPUT = '/home/edd/Proyectos/Edd-OS/projects/kings-and-queens/'

AGUA  = (115, 195, 180)
AGUAD = (90,  170, 155)
CORAL = (237, 109, 112)
GRIS  = (89,  110, 135)
WHITE = (255, 255, 255)

img = Image.new('RGB', (W, H), AGUA)
d   = ImageDraw.Draw(img)

def ctext(txt, font, y, color):
    bb = d.textbbox((0, 0), txt, font=font)
    tw = bb[2] - bb[0]
    d.text(((W - tw) // 2, y), txt, fill=color, font=font)

def text_width(txt, font):
    bb = d.textbbox((0, 0), txt, font=font)
    return bb[2] - bb[0]

# ── BACKGROUND: concentric arcs from bottom-right ──────────────────────────
for i in range(18):
    r = 55 + i * 80
    ox, oy = W + 55, H + 55
    shade = max(36, 86 - i * 4)
    c = (min(255, shade + 16), min(255, shade + 72), min(255, shade + 66))
    d.ellipse([ox - r, oy - r, ox + r, oy + r], outline=c, width=1)

# Subtle dot grid — top-left quadrant only
for row in range(9):
    for col in range(6):
        px = 48 + col * 64
        py = 48 + row * 60
        if py < 560 and px < 440:
            d.ellipse([px, py, px + 3, py + 3], fill=AGUAD)

# ── CORAL ARC — top-left corner decoration ─────────────────────────────────
arc_data = [
    (22, 298, (200, 72, 74)),
    (12, 338, CORAL),
    (6,  370, (248, 138, 140)),
    (2,  400, (255, 170, 172)),
]
for width, r, color in arc_data:
    d.arc([-r + 46, -r + 46, r + 46, r + 46], start=0, end=90, fill=color, width=width)

# ── CROWN ───────────────────────────────────────────────────────────────────
cx = W // 2   # 540
CB = 410      # crown base bottom y
BH = 64       # base height
bx1, bx2 = cx - 162, cx + 162   # 378, 702
by1 = CB - BH                    # 346
by2 = CB                         # 410

# Peak tips (left, center, right)
L = (cx - 116, by1 - 130)
C = (cx,        by1 - 188)
R = (cx + 116, by1 - 130)

# Three peaked triangles
d.polygon([(bx1, by1), (cx - 52, by1), L], fill=WHITE)
d.polygon([(cx - 47, by1), (cx + 47, by1), C], fill=WHITE)
d.polygon([(cx + 52, by1), (bx2, by1), R], fill=WHITE)

# Base rectangle
d.rectangle([bx1, by1, bx2, by2], fill=WHITE)

# Coral accent band on base
d.rectangle([bx1, by1 + 19, bx2, by1 + 31], fill=CORAL)

# Three bottom notches (AGUA → cuts into white to form crenellations)
nw, nh = 46, 18
for nx in [bx1 + 28, cx - nw // 2, bx2 - nw - 28]:
    d.rectangle([nx, by2 - nh, nx + nw, by2 + 2], fill=AGUA)

# Jewels on peak tips: white circle with coral center
for tip in [L, C, R]:
    d.ellipse([tip[0]-15, tip[1]-15, tip[0]+15, tip[1]+15], fill=WHITE)
    d.ellipse([tip[0]-7,  tip[1]-7,  tip[0]+7,  tip[1]+7],  fill=CORAL)

# ── BRAND NAME ──────────────────────────────────────────────────────────────
fBrand   = ImageFont.truetype(FONTS + 'BigShoulders-Bold.ttf', 88)
fTagline = ImageFont.truetype(FONTS + 'InstrumentSans-Regular.ttf', 24)

ctext("KINGS & QUEENS",                   fBrand,   424, WHITE)
ctext("E N G L I S H   L E A R N I N G", fTagline, 530, WHITE)

# Dual hairline — white + coral
d.line([(190, 568), (W - 190, 568)], fill=WHITE, width=1)
d.line([(232, 572), (W - 232, 572)], fill=CORAL, width=1)

# ── WHITE CARD ──────────────────────────────────────────────────────────────
CY = 596
d.rectangle([0, CY, W, H], fill=WHITE)
# Coral accent strip at card top
d.rectangle([0, CY, W, CY + 6], fill=CORAL)

# Headline: INSCRIPCION / ABIERTA
fIns = ImageFont.truetype(FONTS + 'BigShoulders-Bold.ttf', 70)
fAb  = ImageFont.truetype(FONTS + 'BigShoulders-Bold.ttf', 108)

ctext("INSCRIPCION", fIns, CY + 26,  GRIS)
ctext("ABIERTA",     fAb,  CY + 114, CORAL)

# Dynamic positioning based on actual bbox
bb_ab = d.textbbox((0, 0), "ABIERTA", font=fAb)
ab_bottom = CY + 114 + (bb_ab[3] - bb_ab[1])  # ~795

# Serif sub-headline
fSub2 = ImageFont.truetype(FONTS + 'Lora-Italic.ttf', 32)
sub_y = ab_bottom + 28
ctext("Clases Virtuales de Ingles", fSub2, sub_y, GRIS)

bb_sub = d.textbbox((0, 0), "Clases Virtuales de Ingles", font=fSub2)
sub_bottom = sub_y + (bb_sub[3] - bb_sub[1])   # ~877

# Ornamental divider: thin agua line + small coral diamond center
div_y = sub_bottom + 24
d.line([(80, div_y), (W - 80, div_y)], fill=AGUA, width=2)
# Center diamond
dm = 8  # diamond half-size
d.polygon([(W//2, div_y - dm), (W//2 + dm, div_y),
           (W//2, div_y + dm), (W//2 - dm, div_y)], fill=CORAL)

# Bullet info points
fInfo  = ImageFont.truetype(FONTS + 'InstrumentSans-Regular.ttf', 27)
fInfoB = ImageFont.truetype(FONTS + 'InstrumentSans-Bold.ttf',    27)

bullets = [
    ("Todos los niveles  -  Principiante a Avanzado", False),
    ("100% Online  -  Clases en vivo por Zoom",        False),
    ("Cupos limitados  -  Reserva tu lugar ahora!",    True),
]
bullet_y0 = div_y + 22
for i, (txt, bold) in enumerate(bullets):
    by = bullet_y0 + i * 50
    # Bullet: small coral circle
    d.ellipse([80, by + 10, 94, by + 24], fill=CORAL)
    # Small inner white dot
    d.ellipse([85, by + 15, 89, by + 19], fill=WHITE)
    color = CORAL if bold else GRIS
    f     = fInfoB if bold else fInfo
    d.text((114, by), txt, fill=color, font=f)

last_bullet_y = bullet_y0 + 2 * 50
bb_bullet = d.textbbox((0, 0), bullets[0][0], font=fInfo)
bullet_h   = bb_bullet[3] - bb_bullet[1]
bullets_bottom = last_bullet_y + bullet_h  # ~1035

# CTA Button
BTN_Y = bullets_bottom + 30
d.rectangle([80, BTN_Y, W - 80, BTN_Y + 78], fill=CORAL)
# Thin white inner border
d.rectangle([84, BTN_Y + 4, W - 84, BTN_Y + 74], outline=WHITE, width=1)
fCTA = ImageFont.truetype(FONTS + 'InstrumentSans-Bold.ttf', 29)
ctext("ESCRIBI POR INSTAGRAM PARA INSCRIBIRTE", fCTA, BTN_Y + 25, WHITE)

# Footer
FY = BTN_Y + 78 + 26
d.rectangle([0, FY, W, H], fill=GRIS)
d.line([(0, FY), (W, FY)], fill=AGUA, width=3)

fIG  = ImageFont.truetype(FONTS + 'GeistMono-Regular.ttf', 25)
fLoc = ImageFont.truetype(FONTS + 'InstrumentSans-Regular.ttf', 20)

# Instagram icon placeholder: small coral circle
ig_bb = d.textbbox((0, 0), "@kingsandqueensenglish", font=fIG)
ig_w = ig_bb[2] - ig_bb[0]
ig_x = (W - ig_w) // 2
ig_y = FY + 26
d.text((ig_x, ig_y), "@kingsandqueensenglish", fill=WHITE, font=fIG)

ctext("Bahia Blanca  -  Argentina", fLoc, FY + 66, AGUA)

# Tiny crown icon in footer center-bottom
tc_cx = W // 2
tc_y = FY + 110
tc_w, tc_h = 28, 18
d.polygon([(tc_cx - tc_w, tc_y + tc_h),
           (tc_cx - tc_w//2, tc_y + tc_h//2),
           (tc_cx,           tc_y),
           (tc_cx + tc_w//2, tc_y + tc_h//2),
           (tc_cx + tc_w,    tc_y + tc_h)], outline=AGUA, width=1)

# ── SAVE ────────────────────────────────────────────────────────────────────
os.makedirs(OUTPUT, exist_ok=True)
out_path = OUTPUT + 'poster_inscripcion_kq.png'
img.save(out_path, 'PNG', dpi=(300, 300))
print(f"Guardado: {out_path}")
print(f"Verificacion: BTN_Y={BTN_Y}, FY={FY}, H={H}")
