#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Invoice StudioSoft — Warung Pak Otto (Rp 3.000.000)
Format breakdown ala invoice LEIT: tabel Deskripsi/Item + Total per item,
lalu Subtotal -> Total. Gaya tetap mengikuti template final StudioSoft (Poppins,
safe zone, header biru), tapi tabel memakai breakdown item.
"""
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

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

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 + 190
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 | TOTAL (breakdown ala LEIT) ----
ty = 700
d.rectangle([X0, ty, X1, ty + 60], fill=LIGHT)
d.text((X0 + 20, ty + 16), "NO", font=font("Bold", 24), fill=DARK)
d.text((X0 + 130, ty + 16), "DESKRIPSI", font=font("Bold", 24), fill=DARK)
d.text((X1 - 28, ty + 16), "TOTAL", font=font("Bold", 24), fill=DARK, anchor="ra")

# Breakdown item (total = 3.000.000)
items = [
    ("Maintenance Website & Digital Marketing", "2.400.000"),
    ("Laporan Kinerja & Konsultasi", "600.000"),
]
row_h = 62
ry = ty + 60
for i, (desc, val) in enumerate(items, 1):
    d.line([X0, ry, X1, ry], fill=LINE)
    d.text((X0 + 20, ry + 20), str(i), font=font("Regular", 22), fill=BODY)
    d.text((X0 + 130, ry + 20), desc, font=font("Regular", 22), fill=DARK)
    d.text((X1 - 28, ry + 20), "Rp %s" % val, font=font("SemiBold", 22), fill=DARK, anchor="ra")
    ry += row_h
d.line([X0, ry, X1, ry], fill=LINE)

# ---- Subtotal ----
sub_y = ry + 10
d.text((X0 + 130, sub_y), "Subtotal", font=font("Bold", 22), fill=GRAY)
d.text((X1 - 28, sub_y), "Rp 3.000.000", font=font("SemiBold", 22), fill=DARK, anchor="ra")
sub_y += 40
d.text((X0 + 130, sub_y), "Total (Termasuk Pajak)", font=font("Bold", 22), fill=GRAY)
d.text((X1 - 28, sub_y), "Rp 3.000.000", font=font("SemiBold", 22), fill=DARK, anchor="ra")

# ---- Total box ----
sub_y += 14
kiri_total = X1 - 520
d.rectangle([kiri_total, sub_y, X1, sub_y + 86], fill=BLUE)
d.text((kiri_total + 20, sub_y + 25), "TOTAL AMOUNT", font=font("Bold", 24), fill=(255, 255, 255))
d.text((X1 - 28, sub_y + 23), "Rp 3.000.000", font=font("Bold", 26), fill=(255, 255, 255), anchor="ra")

# ---- Terbilang ----
ter_y = sub_y + 100
d.text((X0, ter_y), "Terbilang: Tiga Juta Rupiah", font=font("Regular", 20), fill=BODY)

# ---- Payment ----
fy = max(H - SAFE_BOTTOM - 330, ter_y + 70)
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_breakdown.png"
img.save(png_out)
pdf_out = "Invoice_112_Warung_Pak_Otto_breakdown.pdf"
img_rgb = img.convert("RGB")
img_rgb.save(pdf_out, "PDF", resolution=150.0)
print("OK ->", png_out)
print("OK ->", pdf_out)
