from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageEnhance
from pathlib import Path
import zipfile, textwrap

DATE = '2026-08-20'
BASE = Path('/root/.hermes/profiles/davinci/cache/images/openai_codex_gpt-image-2-high_20260820_080226_5250b9d1.png')
LOGO = Path('/root/agilizer_assets/agilizer_logo_oficial_clean_1000.png')
FONT_HEAD = Path('/root/agilizer_assets/fonts/Anton-Regular.ttf')
FONT_SMALL = Path('/root/agilizer_assets/fonts/BebasNeue-Regular.ttf')
OUTDIR = Path(f'/root/agent_public/files/agilizer/daily/{DATE}')
OUTDIR.mkdir(parents=True, exist_ok=True)
ART = OUTDIR / f'agilizer_post_viral_{DATE}_pra-hoje-da-tempo.png'
CAPTION = OUTDIR / f'legenda_agilizer_post_viral_{DATE}.txt'
ZIP = OUTDIR / f'pack_agilizer_post_viral_{DATE}.zip'

W, H = 1080, 1350
brand_green = (0, 203, 67)
dark = (3, 20, 11)
offwhite = (244, 247, 240)
mint = (124, 255, 208)

img = Image.open(BASE).convert('RGB')
# Crop 1024x1280 from top to preserve negative headline space and mechanic expression.
sw, sh = img.size
crop_h = int(sw * H / W)
y0 = 0
img = img.crop((0, y0, sw, y0 + crop_h)).resize((W, H), Image.Resampling.LANCZOS)

# Editorial grade: reduce saturation slightly, high contrast, subtle dark-green atmosphere.
img = ImageEnhance.Color(img).enhance(0.82)
img = ImageEnhance.Contrast(img).enhance(1.18)
img = ImageEnhance.Brightness(img).enhance(0.92)

# Suppress off-palette warm/blue tones gently via green/dark overlay.
overlay = Image.new('RGBA', (W, H), dark + (0,))
d = ImageDraw.Draw(overlay)
# left/top legibility gradient
for x in range(W):
    alpha = int(max(0, 185 * (1 - x / 760)))
    d.line([(x, 0), (x, H)], fill=dark + (alpha,))
for y in range(H):
    alpha = int(max(0, 90 * (1 - y / 560)))
    d.line([(0, y), (W, y)], fill=dark + (alpha,))
# bottom vignette
for y in range(H):
    alpha = int(max(0, 105 * ((y - 900) / 450))) if y > 900 else 0
    d.line([(0, y), (W, y)], fill=(0, 0, 0, alpha))
img = Image.alpha_composite(img.convert('RGBA'), overlay)

# Subtle brand line block, magazine-like but minimal.
draw = ImageDraw.Draw(img)
draw.rounded_rectangle((70, 62, 315, 108), radius=6, fill=brand_green + (235,))
small_font = ImageFont.truetype(str(FONT_SMALL), 34)
draw.text((88, 66), 'CLÁSSICO DE OFICINA', font=small_font, fill=dark)

head_font = ImageFont.truetype(str(FONT_HEAD), 132)
# headline with planned hierarchy
x = 70
y = 138
tracking = 0
# shadow
def text_line(pos, text, font, fill):
    sx, sy = pos
    draw.text((sx+4, sy+5), text, font=font, fill=(0,0,0,145))
    draw.text((sx, sy), text, font=font, fill=fill)

text_line((x, y), 'PRA HOJE', head_font, brand_green)
text_line((x, y+135), 'DÁ TEMPO?', head_font, offwhite)

# Small signature thought, not CTA commercial
caption_font = ImageFont.truetype(str(FONT_SMALL), 34)
draw.text((72, 430), 'TODO MUNDO DA OFICINA JÁ OUVIU.', font=caption_font, fill=offwhite + (230,))
# Mint detail line
Path
line_y = 486
draw.rounded_rectangle((72, line_y, 265, line_y+7), radius=3, fill=mint + (230,))

# Logo: use original asset, discreet lower-left.
logo = Image.open(LOGO).convert('RGBA')
# make logo fit width 178, preserve alpha
lw = 178
lh = int(logo.height * lw / logo.width)
logo = logo.resize((lw, lh), Image.Resampling.LANCZOS)
# place with a subtle dark pad for contrast only, no distortion
pad = Image.new('RGBA', (lw + 40, lh + 28), (3, 20, 11, 130))
pd = ImageDraw.Draw(pad)
pd.rounded_rectangle((0,0,lw+40,lh+28), radius=12, fill=(3,20,11,130), outline=(0,203,67,55), width=1)
pad.alpha_composite(logo, (20,14))
img.alpha_composite(pad, (64, H - lh - 74))

# Tiny border/accent
bd = ImageDraw.Draw(img)
bd.rectangle((0,0,W-1,H-1), outline=(0,203,67,60), width=2)

img = img.convert('RGB')
img.save(ART, 'PNG', optimize=True)

caption_text = """PRA HOJE DÁ TEMPO? 😅\n\nTem frase que faz o relógio da oficina correr em 3x.\n\nQual é a frase clássica que mais aparece aí na sua oficina?\n\nHashtags sugeridas:\n#oficinamecanica #mecanico #autoeletrico #autocenter #donodeoficina #vidadeoficina #oficinabrasil #setorautomotivo #mecanicaautomotiva #empreendedorismoautomotivo\n"""
CAPTION.write_text(caption_text, encoding='utf-8')

with zipfile.ZipFile(ZIP, 'w', compression=zipfile.ZIP_DEFLATED) as z:
    z.write(ART, ART.name)
    z.write(CAPTION, CAPTION.name)

print(f'ART={ART}')
print(f'CAPTION={CAPTION}')
print(f'ZIP={ZIP}')
print(f'DIM={Image.open(ART).size}')
print(f'BYTES_ART={ART.stat().st_size}')
print(f'BYTES_ZIP={ZIP.stat().st_size}')
