import urllib.request
import tempfile
import os
from fpdf import FPDF
from fpdf.enums import XPos, YPos

pdf = FPDF()

# ===============================
# PAGE 1: COVER PAGE
# ===============================
pdf.add_page()
pdf.set_fill_color(15, 23, 42) # slate-900
pdf.rect(0, 0, 210, 297, 'F')

# Gradient-like top accent (multiple thin lines)
for i in range(10):
    pdf.set_fill_color(99 - i*5, 102 - i*2, 241 + i)
    pdf.rect(0, i, 210, 1, 'F')

logo_url = "https://ui-avatars.com/api/?name=Orbit&background=0f172a&color=38bdf8&size=256&font-size=0.33&bold=true&rounded=true"
req = urllib.request.Request(logo_url, headers={'User-Agent': 'Mozilla/5.0'})
try:
    with urllib.request.urlopen(req) as response:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
            tmp.write(response.read())
            tmp_path = tmp.name
    
    # Center logo: (210 - 40) / 2 = 85
    pdf.image(tmp_path, x=85, y=50, w=40)
    os.remove(tmp_path)
except Exception as e:
    print(f"Logo failed: {e}")

# "PROJECT INTELLIGENCE REPORT"
pdf.set_xy(15, 110)
pdf.set_font('helvetica', 'B', 10)
pdf.set_text_color(56, 189, 248) # sky-400
pdf.cell(0, 5, "PROJECT INTELLIGENCE REPORT", align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)

# Title
pdf.ln(5)
pdf.set_font('helvetica', 'B', 28)
pdf.set_text_color(248, 250, 252)
pdf.cell(0, 12, "E-Commerce Rebuild", align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)

# Description
pdf.ln(5)
pdf.set_font('helvetica', '', 12)
pdf.set_text_color(148, 163, 184) # slate-400
desc = "A complete overhaul of the frontend shopping cart and backend payment integration to support international currencies."
pdf.set_x(35)
pdf.multi_cell(140, 6, desc, align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)

# Bottom Footer (Centered Grid)
pdf.set_draw_color(30, 41, 59) # slate-800
pdf.line(40, 240, 170, 240)

pdf.set_y(248)
col_w = 40
start_x = (210 - (col_w * 3)) / 2  # (210 - 120) / 2 = 45

# Col 1
pdf.set_x(start_x)
pdf.set_font('helvetica', 'B', 7)
pdf.set_text_color(100, 116, 139)
pdf.cell(col_w, 4, "PROJECT KEY", align='C')

# Col 2
pdf.set_x(start_x + col_w)
pdf.cell(col_w, 4, "OWNER", align='C')

# Col 3
pdf.set_x(start_x + col_w * 2)
pdf.cell(col_w, 4, "GENERATED ON", align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)

pdf.set_y(253)
pdf.set_x(start_x)
pdf.set_font('helvetica', 'B', 10)
pdf.set_text_color(248, 250, 252)
pdf.cell(col_w, 5, "ECOMM", align='C')

pdf.set_x(start_x + col_w)
pdf.cell(col_w, 5, "SoulSync", align='C')

pdf.set_x(start_x + col_w * 2)
pdf.cell(col_w, 5, "28 May 2026", align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)


# ===============================
# PAGE X: BACK PAGE
# ===============================
pdf.add_page()
pdf.set_fill_color(15, 23, 42) # slate-900
pdf.rect(0, 0, 210, 297, 'F')

# End of report text
pdf.set_y(130)
pdf.set_font('helvetica', 'B', 20)
pdf.set_text_color(248, 250, 252)
pdf.cell(0, 10, "End of Report", align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)

pdf.ln(5)
pdf.set_font('helvetica', '', 10)
pdf.set_text_color(148, 163, 184)
pdf.cell(0, 5, "Generated securely by Orbit platform", align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)
pdf.cell(0, 5, "Timestamp: 28 May 2026, 10:42 AM", align='C', new_x=XPos.LMARGIN, new_y=YPos.NEXT)

# Bottom decorative line
pdf.set_draw_color(30, 41, 59)
pdf.line(80, 260, 130, 260)

# Optional small logo at bottom
try:
    req = urllib.request.Request(logo_url, headers={'User-Agent': 'Mozilla/5.0'})
    with urllib.request.urlopen(req) as response:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
            tmp.write(response.read())
            tmp_path = tmp.name
    pdf.image(tmp_path, x=95, y=270, w=20)
    os.remove(tmp_path)
except:
    pass

pdf.output('test_cover.pdf')
print("Cover and Back page success!")
