#!/usr/bin/env python3
"""
StudioSoft — Threads Carousel 5 Slides — BG PUTIH
Tema: Apa Itu SEO
1080x1350 (4:5) • BG Putih #FFFFFF • Font Biru #1E40AF
Margin 50px • Konten vertically centered • Font Poppins
"""

from PIL import Image, ImageDraw, ImageFont
import os

OUT = os.path.expanduser("~/.openclaw/workspace-atun/studiosoft_carousel")
os.makedirs(OUT, exist_ok=True)

W, H = 1080, 1350
BG = (255, 255, 255)
FG = (30, 64, 175)
SUB = (100, 130, 200)
M = 50
CONTENT_W = W - 2 * M

FD = os.path.expanduser("~/.fonts")

J100 = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 100)
J85  = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 85)
B36  = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 36)
B28  = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 28)
R28  = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 28)
R24  = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 24)
R20  = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 20)


def fh(d, text, font):
    bb = d.textbbox((0, 0), text, font=font)
    return bb[3] - bb[1]


def fw(d, text, font):
    return d.textbbox((0, 0), text, font=font)[2]


def wrap_lines(d, text, font, max_w):
    words = text.split()
    lines = []
    cur = ""
    for w in words:
        test = cur + " " + w if cur else w
        if d.textbbox((0, 0), test, font=font)[2] <= max_w:
            cur = test
        else:
            if cur:
                lines.append(cur)
            cur = w
    if cur:
        lines.append(cur)
    return lines


def block_h(d, text, font, max_w, lh_mult=1.4):
    lines = wrap_lines(d, text, font, max_w)
    lh = int(fh(d, "A", font) * lh_mult)
    return len(lines) * lh


def draw_block(d, text, x, y, font, fill, max_w, lh_mult=1.35):
    lines = wrap_lines(d, text, font, max_w)
    lh = int(fh(d, "A", font) * lh_mult)
    for line in lines:
        d.text((x, y), line, font=font, fill=fill)
        y += lh
    return y


def divider(d, x, y, w):
    gap = 20
    dash = 12
    start = x
    end = x + w
    t = start
    while t < end:
        e = min(t + dash, end)
        d.line([(t, y), (e, y)], fill=FG, width=3)
        t = e + gap


def header(d, page):
    d.text((M, M), "STUDIOSOFT", font=B36, fill=FG)
    tw = fw(d, "STUDIOSOFT", B36)
    d.text((M + tw + 10, M + 5), page, font=R20, fill=SUB)


def footer(d, left="www.studiosoft.id", right=""):
    fy = H - 120
    d.text((M, fy), left, font=R20, fill=FG)
    if right:
        d.text((W - M, fy), right, font=R20, fill=FG, anchor="rt")


def clean_edges(im):
    px = im.load()
    bg = BG
    for y in range(H - 5, H):
        for x in range(W):
            px[x, y] = bg
    for y in range(H):
        for x in range(W - 3, W):
            px[x, y] = bg


# ==========================================
# SLIDE 1 — COVER
# ==========================================
print("▶ Slide 1 — Cover")
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "01/05")

titles = ["APA ITU", "SEO?", "SEARCH ENGINE", "OPTIMIZATION"]
sub = "Panduan Dasar — Digital Marketing"
items = [
    "☑  Meningkatkan visibilitas di Google",
    "☑  Mendatangkan traffic organik",
    "☑  Investasi jangka panjang",
]

total_h = 0
for t in titles:
    total_h += int(fh(d, t, J100) * 1.15)
total_h += 60 + 5 + 55  # divider gap
sub_h = block_h(d, sub, R28, CONTENT_W)
total_h += sub_h + 25
for item in items:
    total_h += int(fh(d, item, R28) * 1.3)

hdr_h = int(fh(d, "STUDIOSOFT", B36)) + M
usable = H - 2 * M - hdr_h - 20
start_y = M + hdr_h + 20 + (usable - total_h) // 2

ty = start_y
for t in titles:
    d.text((M, ty), t, font=J100, fill=FG)
    ty += int(fh(d, t, J100) * 1.15)

ty += 60
divider(d, M, ty, 400)
ty += 55

ty = draw_block(d, sub, M, ty, R28, SUB, CONTENT_W)
ty += 15

for item in items:
    d.text((M, ty), item, font=R28, fill=FG)
    ty += int(fh(d, item, R28) * 1.3)

footer(d, "www.studiosoft.id", "Swipe →")
clean_edges(im)
im.save(f"{OUT}/01_cover.png")
print("✅ 01_cover.png")

# ==========================================
# SLIDE 2 — APA ITU SEO
# ==========================================
print("▶ Slide 2 — Apa Itu SEO")
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "02/05")

desc = (
    "SEO adalah singkatan dari Search Engine Optimization "
    "— serangkaian strategi dan teknik untuk mengoptimasi website "
    "agar muncul di peringkat teratas hasil pencarian Google, "
    "Bing, dan mesin pencari lainnya."
)
desc2 = (
    "Tujuannya sederhana: ketika calon pelanggan mencari "
    "produk atau layanan yang Anda tawarkan, website Andalah "
    "yang pertama mereka lihat."
)
points = [
    "☑  Bukan iklan — gratis & organik",
    "☑  Hasil bertahan lama",
    "☑  Bangun kredibilitas & trust",
]

total_h = int(fh(d, "APA ITU SEO", J85) * 1.15)
total_h += 60 + 5 + 55
desc_h = block_h(d, desc, R28, CONTENT_W) + block_h(d, desc2, R28, CONTENT_W) + 10
total_h += desc_h + 20
for p in points:
    total_h += int(fh(d, p, R28) * 1.3)

hdr_h = int(fh(d, "STUDIOSOFT", B36)) + M
usable = H - 2 * M - hdr_h - 20
start_y = M + hdr_h + 20 + (usable - total_h) // 2

ty = start_y
d.text((M, ty), "APA ITU SEO", font=J85, fill=FG)
ty += int(fh(d, "APA ITU SEO", J85) * 1.15)

ty += 60
divider(d, M, ty, 300)
ty += 55

ty = draw_block(d, desc, M, ty, R28, FG, CONTENT_W)
ty += 5
ty = draw_block(d, desc2, M, ty, R28, FG, CONTENT_W)
ty += 15

for p in points:
    d.text((M, ty), p, font=R28, fill=FG)
    ty += int(fh(d, p, R28) * 1.3)

footer(d, "www.studiosoft.id")
clean_edges(im)
im.save(f"{OUT}/02_apa_itu_seo.png")
print("✅ 02_apa_itu_seo.png")

# ==========================================
# SLIDE 3 — MENGAPA SEO PENTING
# ==========================================
print("▶ Slide 3 — Mengapa SEO Penting")
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "03/05")

total_h = int(fh(d, "MENGAPA", J100) * 1.15) + int(fh(d, "PENTING?", J100) * 1.15)
total_h += 60 + 5 + 55

stats = (
    "68% pengalaman online dimulai dari mesin pencari.\n"
    "75% pengguna tidak pernah scroll ke halaman 2 Google.\n"
    "Organic search menyumbang 53% traffic website rata-rata."
)
stats_h = block_h(d, stats, R28, CONTENT_W)
total_h += stats_h + 20

bullets = [
    "☑  Traffic organik = hemat biaya iklan",
    "☑  Visitasi konsisten setiap hari",
    "☑  Kepercayaan lebih tinggi dari iklan",
]

for b in bullets:
    total_h += int(fh(d, b, R28) * 1.3)

hdr_h = int(fh(d, "STUDIOSOFT", B36)) + M
usable = H - 2 * M - hdr_h - 20
start_y = M + hdr_h + 20 + (usable - total_h) // 2

ty = start_y
d.text((M, ty), "MENGAPA", font=J100, fill=FG)
ty += int(fh(d, "MENGAPA", J100) * 1.15)
d.text((M, ty), "PENTING?", font=J100, fill=FG)
ty += int(fh(d, "PENTING?", J100) * 1.15)

ty += 60
divider(d, M, ty, 300)
ty += 55

ty = draw_block(d, stats, M, ty, R28, FG, CONTENT_W)
ty += 15

for b in bullets:
    d.text((M, ty), b, font=R28, fill=FG)
    ty += int(fh(d, b, R28) * 1.3)

footer(d, "www.studiosoft.id")
clean_edges(im)
im.save(f"{OUT}/03_mengapa_penting.png")
print("✅ 03_mengapa_penting.png")

# ==========================================
# SLIDE 4 — JENIS SEO
# ==========================================
print("▶ Slide 4 — Jenis SEO")
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "04/05")

total_h = int(fh(d, "3 PILAR", J100) * 1.15) + int(fh(d, "SEO", J100) * 1.15)
total_h += 60 + 5 + 55

seo_types = [
    ("🔍", "On-Page SEO", "Optimasi konten, judul, meta, heading di dalam website."),
    ("🔗", "Off-Page SEO", "Membangun backlink & otoritas dari website lain."),
    ("⚙️", "Technical SEO", "Kecepatan, struktur data, sitemap, dan mobile-friendly."),
]

for icon, title, desc_line in seo_types:
    total_h += int(fh(d, title, B28) * 1.3) + int(fh(d, "X", R24) * 1.3)

hdr_h = int(fh(d, "STUDIOSOFT", B36)) + M
usable = H - 2 * M - hdr_h - 20
start_y = M + hdr_h + 20 + (usable - total_h) // 2

ty = start_y
d.text((M, ty), "3 PILAR", font=J100, fill=FG)
ty += int(fh(d, "3 PILAR", J100) * 1.15)
d.text((M, ty), "SEO", font=J100, fill=FG)
ty += int(fh(d, "SEO", J100) * 1.15)

ty += 60
divider(d, M, ty, 300)
ty += 55

for icon, title, desc_line in seo_types:
    d.text((M, ty), f"{icon}  {title}", font=B28, fill=FG)
    ty += int(fh(d, title, B28) * 1.3)
    ty = draw_block(d, desc_line, M + 45, ty, R24, SUB, CONTENT_W - 45, lh_mult=1.4)
    ty += 15

footer(d, "www.studiosoft.id")
clean_edges(im)
im.save(f"{OUT}/04_jenis_seo.png")
print("✅ 04_jenis_seo.png")

# ==========================================
# SLIDE 5 — CTA
# ==========================================
print("▶ Slide 5 — CTA")
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "05/05")

total_h = int(fh(d, "SIAP", J100) * 1.15) + int(fh(d, "OPTIMASI", J100) * 1.15) + int(fh(d, "SEO?", J100) * 1.15)
total_h += 60 + 5 + 55

cta_text = (
    "SEO bukan ilmu sihir — ini strategi yang terukur.\n"
    "Dengan pendekatan yang tepat, website Anda bisa\n"
    "muncul di halaman pertama Google."
)
cta_h = block_h(d, cta_text, R28, CONTENT_W)
total_h += cta_h + 25

cta_items = [
    "☑  Konsultasi gratis kebutuhan SEO Anda",
    "☑  Audit website tanpa biaya",
    "☑  Timeline & strategi jelas",
]
for item in cta_items:
    total_h += int(fh(d, item, R28) * 1.3)
total_h += 25

cta_box = "Konsultasi Sekarang"
cta_h2 = int(fh(d, cta_box, B28) * 1.5)
total_h += cta_h2 + 5
total_h += int(fh(d, "www.studiosoft.id", R28) * 1.3)

hdr_h = int(fh(d, "STUDIOSOFT", B36)) + M
usable = H - 2 * M - hdr_h - 20
start_y = M + hdr_h + 20 + (usable - total_h) // 2

ty = start_y
d.text((M, ty), "SIAP", font=J100, fill=FG)
ty += int(fh(d, "SIAP", J100) * 1.15)
d.text((M, ty), "OPTIMASI", font=J100, fill=FG)
ty += int(fh(d, "OPTIMASI", J100) * 1.15)
d.text((M, ty), "SEO?", font=J100, fill=FG)
ty += int(fh(d, "SEO?", J100) * 1.15)

ty += 60
divider(d, M, ty, 300)
ty += 55

ty = draw_block(d, cta_text, M, ty, R28, FG, CONTENT_W)
ty += 15

for item in cta_items:
    d.text((M, ty), item, font=R28, fill=FG)
    ty += int(fh(d, item, R28) * 1.3)

ty += 25

# CTA Box
btw = fw(d, cta_box, B28)
bx, by = M, ty
bw = btw + 60
bh = cta_h2 + 10
d.rounded_rectangle([(bx, by), (bx + bw, by + bh)], radius=30, fill=FG)
d.text((bx + 30, by + 5), cta_box, font=B28, fill=BG)
ty += bh + 10

ty += 5
d.text((M, ty), "www.studiosoft.id", font=R28, fill=FG)

footer(d)
clean_edges(im)
im.save(f"{OUT}/05_cta.png")
print("✅ 05_cta.png")

print("\n📦 Selesai! 5 slide tersimpan di:", OUT)
print("  📄 01_cover.png")
print("  📄 02_apa_itu_seo.png")
print("  📄 03_mengapa_penting.png")
print("  📄 04_jenis_seo.png")
print("  📄 05_cta.png")
