#!/usr/bin/env python3
"""
StudioSoft — IG Carousel Artikel 1 (5 Slides)
"Kenapa Bisnis Masih Butuh Digital Marketing di 2026?"
BG Biru #1E40AF • Judul 100px Poppins Bold • Body 28px Poppins Regular
1080x1350 (4:5) • Margin 50px • Konten vertical-middle
"""

from PIL import Image, ImageDraw, ImageFont
import os

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

W, H = 1080, 1350
BG = (30, 64, 175)
WHITE = (255, 255, 255)
SOFT = (180, 200, 240)
M = 50
CONTENT_W = W - 2 * M  # 980

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

J100 = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 100)
B36 = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 36)
B44 = ImageFont.truetype(f"{FD}/Poppins-Bold.ttf", 44)
SB30 = ImageFont.truetype(f"{FD}/Poppins-SemiBold.ttf", 30)
R28 = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 28)
R22 = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 22)
R20 = ImageFont.truetype(f"{FD}/Poppins-Regular.ttf", 20)


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


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


def wrap(d, text, font, mx):
    lines = []
    for p in text.split("\n"):
        ws = p.split()
        cur = ""
        for w in ws:
            t = (cur + " " + w) if cur else w
            if fw(d, t, font) > mx and cur:
                lines.append(cur)
                cur = w
            else:
                cur = t
        if cur:
            lines.append(cur)
    return lines


def block_h(d, content, font, mx, ls=1.35):
    total = 0
    for line in wrap(d, content, font, mx):
        total += int(fh(d, line, font) * ls)
    return total


def draw_block(d, text, x, y, font, fill, mx, ls=1.35):
    for line in wrap(d, text, font, mx):
        lh = fh(d, line, font)
        d.text((x, y), line, font=font, fill=fill)
        y += int(lh * ls)
    return y


def header(d, page):
    d.text((M, M), "STUDIOSOFT", font=B36, fill=WHITE)
    d.text((W - M, M), page, font=R22, fill=WHITE, anchor="rt")


def divider(d, x, y, w=300):
    dx = x
    while dx < x + w:
        e = min(dx + 14, x + w)
        d.line([(dx, y), (e, y)], fill=WHITE, width=2)
        dx += 24


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


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


def save(im, name):
    im.save(f"{OUT}/{name}")
    print(f"✅ {name}")


hdr_h = fh(ImageDraw.Draw(Image.new("RGB", (W, H))), "STUDIOSOFT", B36)


def usable_h():
    return H - 2 * M - hdr_h - 20


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

titles = ["KENAPA BISNIS", "MASIH BUTUH", "DIGITAL", "MARKETING", "DI 2026?"]
sub = "Konsumen mencari di Google sebelum membeli.\nBisnis yang tidak terlihat, tidak terpilih."

total_h = 0
for t in titles:
    total_h += int(fh(d, t, J100) * 1.12)
total_h += 60 + 5 + 30
sub_h = block_h(d, sub, R28, CONTENT_W)
total_h += sub_h

start_y = M + hdr_h + 20 + (usable_h() - total_h) // 2
ty = start_y
for t in titles:
    d.text((M, ty), t, font=J100, fill=WHITE)
    ty += int(fh(d, t, J100) * 1.12)

ty += 30
divider(d, M, ty, 300)
ty += 25
ty = draw_block(d, sub, M, ty, R28, SOFT, CONTENT_W)

footer(d, "www.studiosoft.id", "Swipe →")
clean_edges(im)
save(im, "01_cover.png")

# ==========================================
# SLIDE 2 — FAKTA
# ==========================================
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "02/05")

total_h = 0
for t in ["ANGKA YANG", "BICARA"]:
    total_h += int(fh(d, t, J100) * 1.12)
total_h += 60 + 5 + 30

facts = [
    ("81%", "konsumen riset online sebelum membeli"),
    ("70%", "perjalanan beli dimulai dari mesin pencari"),
    ("63%", "orang mengecek bisnis lewat Google Maps"),
    ("76%", "percaya pada bisnis yang punya website profesional"),
]
for num, desc in facts:
    total_h += int(fh(d, num, B44) * 1.25) + int(fh(d, desc, R28) * 1.2) + 12

note = "Sumber: riset perilaku konsumen digital, 2026"
total_h += 20 + block_h(d, note, R22, CONTENT_W)

start_y = M + hdr_h + 20 + (usable_h() - total_h) // 2
ty = start_y
for t in ["ANGKA YANG", "BICARA"]:
    d.text((M, ty), t, font=J100, fill=WHITE)
    ty += int(fh(d, t, J100) * 1.12)

ty += 30
divider(d, M, ty, 300)
ty += 25

for num, desc in facts:
    d.text((M, ty), num, font=B44, fill=WHITE)
    ty += int(fh(d, num, B44) * 1.25)
    ty = draw_block(d, desc, M, ty, R28, SOFT, CONTENT_W)
    ty += 12

ty += 10
d.text((M, ty), note, font=R22, fill=SOFT)

footer(d, "www.studiosoft.id")
clean_edges(im)
save(im, "02_fakta.png")

# ==========================================
# SLIDE 3 — 3 ALASAN
# ==========================================
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "03/05")

total_h = 0
for t in ["3 ALASAN", "KENAPA?"]:
    total_h += int(fh(d, t, J100) * 1.12)
total_h += 60 + 5 + 30

reasons = [
    ("1", "Perilaku konsumen pindah ke digital", "Riset, banding, dan beli — semua di ponsel."),
    ("2", "Kompetitor sudah online lebih dulu", "Yang muncul pertama yang paling diingat."),
    ("3", "AI & algoritma mengubah cara ditemukan", "Brand yang punya data, menang di 2026."),
]
for num, title, desc in reasons:
    total_h += int(fh(d, title, SB30) * 1.3) + int(fh(d, desc, R28) * 1.2) + 10

start_y = M + hdr_h + 20 + (usable_h() - total_h) // 2
ty = start_y
for t in ["3 ALASAN", "KENAPA?"]:
    d.text((M, ty), t, font=J100, fill=WHITE)
    ty += int(fh(d, t, J100) * 1.12)

ty += 30
divider(d, M, ty, 300)
ty += 25

for num, title, desc in reasons:
    d.text((M, ty), num, font=B44, fill=WHITE)
    ty += int(fh(d, num, B44) * 1.25)
    ty = draw_block(d, title, M, ty, SB30, WHITE, CONTENT_W)
    ty = draw_block(d, desc, M, ty, R28, SOFT, CONTENT_W)
    ty += 18

footer(d, "www.studiosoft.id")
clean_edges(im)
save(im, "03_alasan.png")

# ==========================================
# SLIDE 4 — RISIKO
# ==========================================
im = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(im)
header(d, "04/05")

total_h = 0
for t in ["KALAU", "TELAT?"]:
    total_h += int(fh(d, t, J100) * 1.12)
total_h += 60 + 5 + 30

risks = [
    "☑  Pelanggan berpindah ke kompetitor yang lebih terlihat",
    "☑  Biaya iklan terus naik setiap tahun",
    "☑  Sulit bersaing dengan brand yang punya data",
    "☑  Bisnis terasa \u201ctidak ada\u201d di mata konsumen digital",
]
for r in risks:
    total_h += int(fh(d, r, R28) * 1.3)

closing = "Digital marketing bukan lagi pilihan —\nsudah menjadi kebutuhan."
total_h += 30 + block_h(d, closing, R28, CONTENT_W)

start_y = M + hdr_h + 20 + (usable_h() - total_h) // 2
ty = start_y
for t in ["KALAU", "TELAT?"]:
    d.text((M, ty), t, font=J100, fill=WHITE)
    ty += int(fh(d, t, J100) * 1.12)

ty += 30
divider(d, M, ty, 300)
ty += 25

for r in risks:
    d.text((M, ty), r, font=R28, fill=WHITE)
    ty += int(fh(d, r, R28) * 1.3)

ty += 20
ty = draw_block(d, closing, M, ty, R28, SOFT, CONTENT_W)

footer(d, "www.studiosoft.id")
clean_edges(im)
save(im, "04_risiko.png")

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

total_h = 0
for t in ["SIAP TUMBUH", "BERBASIS", "DATA?"]:
    total_h += int(fh(d, t, J100) * 1.12)
total_h += 60 + 5 + 30

desc = "StudioSoft membantu bisnis Anda tampil,\nditemukan, dan dipercaya pelanggan digital."
total_h += block_h(d, desc, R28, CONTENT_W) + 20

items = [
    "☑  Strategi digital terukur",
    "☑  SEO & website yang menghasilkan",
    "☑  Iklan & konten yang efisien",
]
for it in items:
    total_h += int(fh(d, it, R28) * 1.3)

cta = "Konsultasi gratis → www.studiosoft.id"
total_h += 30 + block_h(d, cta, SB30, CONTENT_W)

start_y = M + hdr_h + 20 + (usable_h() - total_h) // 2
ty = start_y
for t in ["SIAP TUMBUH", "BERBASIS", "DATA?"]:
    d.text((M, ty), t, font=J100, fill=WHITE)
    ty += int(fh(d, t, J100) * 1.12)

ty += 30
divider(d, M, ty, 300)
ty += 25

ty = draw_block(d, desc, M, ty, R28, SOFT, CONTENT_W)
ty += 15

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

ty += 20
ty = draw_block(d, cta, M, ty, SB30, WHITE, CONTENT_W)

footer(d, "www.studiosoft.id")
clean_edges(im)
save(im, "05_cta.png")

print("\n📦 Selesai! 5 slides artikel 1.")
