#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Invoice StudioSoft — Warung Pak Otto (Rp 3.000.000)
Kolom DESKRIPSI berisi banyak penjelasan pekerjaan (daftar tugas maintenance),
TANPA harga per item. Harga cuma di TOTAL di akhir.
"""
from PIL import Image, ImageDraw, ImageFont

W, H = 1240, 1754  # A4 @ 150dpi
SAFE = 90
SAFE_BOTTOM = 59

BG = (255, 255, 255)
BLUE = (30, 64, 175)
DARK = (15, 23, 42)
BODY = (71, 85, 105)
GRAY = (100, 116, 139)
LIGHT = (241, 245, 249)
LINE = (203, 213, 225)

X0, X1 = SAFE, W - SAFE
COL_DESC_X = X0 + 130   # mulai kolom deskripsi

def font(name, size):
    return ImageFont.truetype("Poppins-%s.ttf" % name, size)

def wrap(text, f, maxw):
    words = text.split(" ")
    lines, cur = [], ""
    for w in words:
        t = (cur + " " + w).strip()
        if d.textlength(t, font=f) <= maxw:
            cur = t
        else:
            if cur:
                lines.append(cur)
            cur = w
    if cur:
        lines.append(cur)
    return lines

img = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(img)

# ---- Header band ----
d.rectangle([0, 0, W, 250], fill=BLUE)

logo = Image.open("assets_studiosoft_logo.png").convert("RGBA")
lpix = logo.load()
for x in range(logo.width):
    for y in range(logo.height):
        r, g, b, a = lpix[x, y]
        if a > 0:
            lpix[x, y] = (255, 255, 255, a)
logo_w = 200
logo_h = int(logo.height * logo_w / logo.width)
logo = logo.resize((logo_w, logo_h), Image.LANCZOS)
img.paste(logo, (X0, 75), logo)

d.text((X1, 60), "INVOICE", font=font("Bold", 56), fill=(255, 255, 255), anchor="ra")
d.text((X1, 138), "No. INV/2026/0112", font=font("Regular", 25), fill=(219, 234, 254), anchor="ra")
d.text((X1, 182), "Surabaya, 18 Agustus 2026", font=font("Regular", 21), fill=(219, 234, 254), anchor="ra")

# ---- From / Bill To ----
y = 320
col2x = X0 + 520
d.text((X0, y), "DARI", font=font("Bold", 20), fill=GRAY)
d.text((X0, y + 34), "StudioSoft", font=font("Bold", 32), fill=DARK)
for i, t in enumerate(["Jl. Mangga 3 E-346 B, Surabaya",
                       "Email: halo@studiosoft.id",
                       "WA: 0819-3500-0624"]):
    d.text((X0, y + 88 + i * 33), t, font=font("Regular", 22), fill=BODY)

d.text((col2x, y), "KEPADA", font=font("Bold", 20), fill=GRAY)
d.text((col2x, y + 34), "Warung Pak Otto", font=font("Bold", 32), fill=DARK)
for i, t in enumerate(["www.warungpakotto.com", "", ""]):
    d.text((col2x, y + 88 + i * 33), t, font=font("Regular", 22), fill=BODY)

# ---- Info invoice: periode layanan ----
iy = y + 195
d.text((X0, iy), "Periode Layanan:", font=font("Bold", 20), fill=GRAY)
d.text((X0 + 260, iy), "Agustus 2026", font=font("Regular", 22), fill=BODY)

# ---- Table: NO | DESKRIPSI (isi panjang, tanpa harga per item) ----
ty = 690
desc_width = X1 - COL_DESC_X - 20
# Header
d.rectangle([X0, ty, X1, ty + 60], fill=LIGHT)
d.text((X0 + 20, ty + 16), "NO", font=font("Bold", 24), fill=DARK)
d.text((COL_DESC_X, ty + 16), "DESKRIPSI", font=font("Bold", 24), fill=DARK)

# Isi deskripsi (banyak penjelasan pekerjaan)
desc_lines = [
    ("Maintenance Digital Marketing — www.warungpakotto.com", True),
    ("• Maintenance & update konten website", False),
    ("• Pengelolaan akun sosial media", False),
    ("• Optimasi SEO (search engine optimization)", False),
    ("• Pembuatan konten & copywriting", False),
    ("• Monitoring performa dan laporan bulanan", False),
    ("• Konsultasi & pendampingan digital marketing", False),
]

f_title = font("SemiBold", 22)
f_body = font("Regular", 21)
line_h = 34
ry = ty + 60
# gambar manual
y_cell = ry
for txt, bold in desc_lines:
    f = f_title if bold else f_body
    wrapped = wrap(txt, f, desc_width)
    for wl in wrapped:
        d.text((COL_DESC_X, y_cell), wl, font=f, fill=DARK)
        y_cell += line_h
    y_cell += 6
ry = y_cell

# garis bawah tabel
d.line([X0, ry, X1, ry], fill=LINE)

# ---- Total (cuma di akhir) ----
ty2 = ry + 30
kiri_total = X1 - 520
d.rectangle([kiri_total, ty2, X1, ty2 + 86], fill=LIGHT)
d.text((kiri_total + 20, ty2 + 25), "TOTAL", font=font("Bold", 26), fill=DARK)
d.text((X1 - 28, ty2 + 23), "Rp 3.000.000", font=font("Bold", 26), fill=BLUE, anchor="ra")

# ---- Payment ----
fy = max(H - SAFE_BOTTOM - 330, ty2 + 86 + 45)
d.text((X0, fy), "PEMBAYARAN", font=font("Bold", 20), fill=GRAY)
d.text((X0, fy + 36), "Transfer ke (a.n. Uky Yanuhandoko):", font=font("SemiBold", 22), fill=DARK)
bank_lines = [
    "1. BCA 6750379638   (Rungkut Mapan, Surabaya)",
    "2. Mandiri 142-00-1068384-4   (Bratang Binangun, Surabaya)",
]
for i, t in enumerate(bank_lines):
    d.text((X0, fy + 78 + i * 34), t, font=font("Regular", 21), fill=BODY)

# ---- Signature ----
d.text((X1 - 320, fy), "Hormat kami,", font=font("Regular", 22), fill=BODY)
try:
    ttd = Image.open("ttd_uky.png").convert("RGBA")
    ttd.thumbnail((260, 120), Image.LANCZOS)
    img.paste(ttd, (X1 - 320, fy + 30), ttd)
except Exception:
    pass
d.line([X1 - 320, fy + 180, X1 - 40, fy + 180], fill=LINE)
d.text((X1 - 320, fy + 195), "StudioSoft", font=font("Bold", 26), fill=DARK)

png_out = "Invoice_112_Warung_Pak_Otto_v2.png"
img.save(png_out)
pdf_out = "Invoice_112_Warung_Pak_Otto_v2.pdf"
img_rgb = img.convert("RGB")
img_rgb.save(pdf_out, "PDF", resolution=150.0)
print("OK ->", png_out)
print("OK ->", pdf_out)
