#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""StudioSoft Invoice — TSC (The Skin Code Clinic) Agustus 2026 v2
- Tanpa baris JATUH TEMPO
- Item list: 5 item, 'Maintenance Social Media Instagram' di urutan no.2
Base: generate_invoices_swiss.py (Swiss minimalis)
"""
from fpdf import FPDF
import os

OUTDIR = os.path.dirname(os.path.abspath(__file__))

COMPANY = "CV. KREASI STUDIOSOFT"
COMP_ALAMAT = "Jl. Mangga 3 E-346 B. Pondok Tjandra. Sidoarjo"
COMP_TELP = "031-71847536 / 081935000624"

BANKS = [
    ("BCA", "Rungkut Mapan, Surabaya", "6750379638", "Uky Yanuhandoko S. Kom"),
    ("Mandiri", "Bratang Binangun, Surabaya", "142-00-1068384-4", "Uky Yanuhandoko S. Kom"),
]

INVOICE_NO = 103
YEAR = "2026"
MONTH_ROMAN = "VIII"
TGL_KOTA = "Surabaya, 1 Agustus 2026"
PERIODE = "Agustus 2026"
TERMIN = "100% Maintenance Fee / Monthly"

NAMA = "TSC (The Skin Code Clinic)"
KOTA = "Krian"
TOTAL = 3000000

ITEMS = [
    "Website Maintenance",
    "Maintenance Social Media Instagram",
    "Input Konten",
    "SEO",
    "Optimasi Campaign Insight",
]

INK = (20, 20, 20)
GRAY = (120, 120, 120)
LIGHT = (235, 235, 235)


class Invoice(FPDF):
    def footer(self):
        self.set_y(-14)
        self.set_font("Helvetica", "", 7.5)
        self.set_text_color(*GRAY)
        self.cell(0, 8, f"{self.page_no()}", align="C")


def rupiah(n):
    return "Rp {:,.0f}".format(n).replace(",", ".")


def add_invoice(no):
    pdf = Invoice()
    pdf.add_page("P", (210, 297))
    pdf.set_auto_page_break(auto=True, margin=22)
    pdf.set_margins(22, 20, 22)
    W = 210 - 44

    # ===== BAR ATAS tipis (aksen Swiss) =====
    pdf.set_fill_color(*INK)
    pdf.rect(22, 16, W, 1.2, "F")

    # ===== HEADER: logo kiri, judul INVOICE kanan =====
    logo = os.path.join(OUTDIR, "assets_studiosoft_logo.png")
    try:
        pdf.image(logo, x=22, y=21, w=20)
    except Exception:
        pass
    pdf.set_xy(W - 30 + 22, 20)
    pdf.set_font("Helvetica", "B", 28)
    pdf.set_text_color(*INK)
    pdf.cell(30, 11, "INVOICE", align="R")
    pdf.set_xy(W - 30 + 22, 32)
    pdf.set_font("Helvetica", "", 8)
    pdf.set_text_color(*GRAY)
    pdf.cell(30, 5, f"No. {no}/INV/{MONTH_ROMAN}/{YEAR}", align="R")

    # ===== blok pembuka (dari & untuk) — TANPA JATUH TEMPO =====
    pdf.set_xy(22, 50)
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(14, 4, "FROM")
    pdf.set_font("Helvetica", "B", 8.5)
    pdf.set_text_color(*INK)
    pdf.cell(66, 4, COMPANY)
    pdf.cell(10, 4, "")
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(14, 4, "TANGGAL")
    pdf.set_font("Helvetica", "B", 8.5)
    pdf.set_text_color(*INK)
    pdf.cell(40, 4, TGL_KOTA)
    pdf.ln(5)
    pdf.set_xy(22, 55)
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(14, 4, "")
    pdf.set_font("Helvetica", "", 8)
    pdf.set_text_color(*INK)
    pdf.cell(66, 4, COMP_ALAMAT)
    pdf.cell(10, 4, "")
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(14, 4, "PERIODE")
    pdf.set_font("Helvetica", "B", 8.5)
    pdf.set_text_color(*INK)
    pdf.cell(40, 4, PERIODE)
    pdf.ln(5)
    pdf.set_xy(22, 60)
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(14, 4, "")
    pdf.set_font("Helvetica", "", 8)
    pdf.set_text_color(*INK)
    pdf.cell(66, 4, COMP_TELP)
    pdf.cell(10, 4, "")
    pdf.cell(40, 4, "")

    # ===== KEPADA (untuk) =====
    pdf.set_xy(22, 74)
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(14, 4, "UNTUK")
    pdf.set_font("Helvetica", "", 8.5)
    pdf.set_text_color(*INK)
    pdf.cell(60, 4, "Pimpinan")
    pdf.ln(5)
    pdf.set_xy(22, 79)
    pdf.cell(14, 4, "")
    pdf.set_font("Helvetica", "B", 10.5)
    pdf.cell(120, 6, NAMA.upper())
    pdf.ln(6)
    pdf.set_xy(22, 85)
    pdf.cell(14, 4, "")
    pdf.set_font("Helvetica", "", 8)
    pdf.set_text_color(*GRAY)
    pdf.cell(70, 4, KOTA)

    # ===== SUBJECT =====
    pdf.set_xy(22, 98)
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(22, 5, "SUBJECT")
    pdf.set_font("Helvetica", "B", 9)
    pdf.set_text_color(*INK)
    pdf.cell(0, 5, "DIGITAL MARKETING FEE MAINTENANCE")
    pdf.ln(5.5)
    pdf.set_xy(22, 104)
    pdf.set_font("Helvetica", "", 8)
    pdf.set_text_color(*GRAY)
    pdf.cell(W, 4, "Relating to the above subject, we estimate as follows.")
    pdf.ln(5)
    pdf.set_xy(22, 109)
    pdf.set_font("Helvetica", "B", 8)
    pdf.set_text_color(*INK)
    pdf.cell(W, 4, f"Termin Pembayaran : {TERMIN}")

    # ===== TABEL ITEM (Swiss: hairline, no heavy box) =====
    col_no = 12
    col_item = W - col_no - 60
    col_total = 60
    x0 = 22
    ty = 118

    pdf.set_fill_color(*LIGHT)
    pdf.set_font("Helvetica", "B", 8)
    pdf.set_text_color(*INK)
    pdf.rect(x0, ty, col_no, 7, "F")
    pdf.set_xy(x0, ty + 1.5)
    pdf.cell(col_no, 5, "No", align="C")
    pdf.rect(x0 + col_no, ty, col_item, 7, "F")
    pdf.set_xy(x0 + col_no, ty + 1.5)
    pdf.cell(col_item, 5, "Item (s)", align="L")
    pdf.rect(x0 + col_no + col_item, ty, col_total, 7, "F")
    pdf.set_xy(x0 + col_no + col_item, ty + 1.5)
    pdf.cell(col_total, 5, "Total", align="R")

    ty += 7
    pdf.set_font("Helvetica", "", 10)
    row_h = 9
    total_shown = False
    for idx, item in enumerate(ITEMS, start=1):
        pdf.set_draw_color(*LIGHT)
        pdf.line(x0, ty, x0 + W, ty)
        tot_txt = rupiah(TOTAL) if not total_shown else ""
        if tot_txt:
            total_shown = True
        yc = ty + (row_h - 5) / 2
        pdf.set_text_color(*INK)
        pdf.set_xy(x0, yc)
        pdf.set_font("Helvetica", "", 8.5)
        pdf.cell(col_no, 5, str(idx), align="C")
        pdf.set_font("Helvetica", "B", 9.5)
        pdf.set_xy(x0 + col_no, yc)
        pdf.cell(col_item, 5, item, align="L")
        pdf.set_font("Helvetica", "B", 9.5)
        pdf.set_xy(x0 + col_no + col_item, yc)
        pdf.cell(col_total, 5, tot_txt, align="R")
        ty += row_h

    # total amount (baris tebal terakhir)
    pdf.set_fill_color(*INK)
    pdf.rect(x0, ty, W, 10, "F")
    pdf.set_font("Helvetica", "B", 8)
    pdf.set_text_color(255, 255, 255)
    pdf.set_xy(x0, ty + 2.5)
    pdf.cell(W - col_total, 5, "TOTAL AMOUNT", align="L")
    pdf.set_font("Helvetica", "B", 11)
    pdf.set_xy(x0 + W - col_total, ty + 2)
    pdf.cell(col_total, 6, rupiah(TOTAL), align="R")

    ty += 10 + 10

    # ===== BANK =====
    pdf.set_text_color(*INK)
    pdf.set_font("Helvetica", "B", 7.5)
    pdf.set_xy(x0, ty)
    pdf.cell(W, 4, "PEMBAYARAN TRANSFER KE")
    pdf.ln(3)
    yb = pdf.get_y() + 1
    for bank, cab, acc, name in BANKS:
        pdf.set_font("Helvetica", "B", 8.5)
        pdf.set_xy(x0, yb)
        pdf.cell(25, 5, bank)
        pdf.set_font("Helvetica", "", 8)
        pdf.set_text_color(*GRAY)
        pdf.cell(55, 5, cab)
        pdf.set_text_color(*INK)
        pdf.cell(42, 5, acc)
        pdf.cell(0, 5, name)
        yb += 6
    pdf.line(x0, yb + 1, x0 + W, yb + 1)

    # ===== TANDA TANGAN =====
    sy = yb + 14
    pdf.set_xy(W - 80 + 22, sy)
    pdf.set_font("Helvetica", "B", 8.5)
    pdf.set_text_color(*INK)
    pdf.cell(80, 5, "CV. KREASI STUDIOSOFT", align="R")
    pdf.set_xy(W - 80 + 22, sy + 13)
    pdf.set_font("Helvetica", "", 8.5)
    pdf.cell(80, 5, "Uky Yanuhandoko", align="R")
    pdf.set_xy(W - 80 + 22, sy + 18)
    pdf.set_font("Helvetica", "", 7.5)
    pdf.set_text_color(*GRAY)
    pdf.cell(80, 5, "Project Manager", align="R")

    fname = "Invoice_%d_TSC_The_Skin_Code_Clinic.pdf" % no
    path = os.path.join(OUTDIR, fname)
    pdf.output(path)
    return path


if __name__ == "__main__":
    p = add_invoice(INVOICE_NO)
    print("GENERATED:", p)
