import re, tempfile, os, urllib.request

def render_ticket(pdf, t, get_time_ago, safe_str, draw_pill):
    pdf.add_page()
    pdf.set_fill_color(248, 250, 252) # F8FAFC Background
    pdf.rect(0, 0, 210, 297, 'F')
    
    # 1. Header Section
    pdf.set_font('helvetica', '', 8)
    pdf.set_text_color(100, 116, 139) # slate-500
    pdf.set_xy(15, 12)
    pdf.cell(0, 5, f"Dashboard > Tickets > {safe_str(t.get('id', ''))}")
    
    # Export Report Button placeholder
    pdf.set_draw_color(226, 232, 240)
    pdf.set_fill_color(255, 255, 255)
    pdf.rect(170, 10, 25, 7, style='DF', round_corners=True, corner_radius=1.5)
    pdf.set_font('helvetica', '', 6)
    pdf.set_text_color(99, 102, 241)
    pdf.set_xy(170, 11)
    pdf.cell(25, 5, "Export Report", align='C')
    
    # Divider
    pdf.set_draw_color(226, 232, 240)
    pdf.line(15, 22, 195, 22)
    
    pdf.set_y(28)
    
    # 2. Hero Ticket Card
    hero_y = pdf.get_y()
    pdf.set_fill_color(255, 255, 255)
    pdf.set_draw_color(226, 232, 240)
    pdf.rect(15, hero_y, 180, 26, style='DF', round_corners=True, corner_radius=3)
    
    # Ticket ID & Priority
    pdf.set_xy(20, hero_y + 4)
    pdf.set_font('helvetica', 'B', 8)
    pdf.set_text_color(99, 102, 241)
    t_id_str = safe_str(t.get('id', ''))
    pdf.cell(50, 4, f"TICKET ID: {t_id_str}")
    
    t_priority = t.get('priority')
    if t_priority:
        pw = pdf.get_string_width(t_priority) + 8
        draw_pill(pdf, 195 - pw - 5, hero_y + 4, pw, 4, t_priority, (254,242,242), (220,38,38))
        
    # Title
    pdf.set_xy(20, hero_y + 9)
    pdf.set_font('helvetica', 'B', 14)
    pdf.set_text_color(15, 23, 42)
    title = t.get('title', '')
    if len(title) > 60: title = title[:57] + "..."
    pdf.cell(100, 6, safe_str(title))
    
    # Bottom Row
    px = 20
    py = hero_y + 18
    t_type = t.get('type')
    if t_type:
        tw = pdf.get_string_width(t_type) + 8
        draw_pill(pdf, px, py, tw, 4, t_type, (224,242,254), (2,132,199))
        px += tw + 4
        
    t_status = t.get('status')
    if t_status:
        sw = pdf.get_string_width(t_status) + 8
        draw_pill(pdf, px, py, sw, 4, t_status, (220,252,231), (22,163,74))
        px += sw + 4
        
    t_sp = str(t.get('story_points') or "-")
    spw = pdf.get_string_width(f"SP: {t_sp}") + 8
    draw_pill(pdf, px, py, spw, 4, f"SP: {t_sp}", (243,232,255), (147,51,234))
    
    pdf.set_y(hero_y + 30)
    
    # 3. Content Area
    col_split_y = pdf.get_y()
    
    # Left Column: Metadata Card
    pdf.set_fill_color(255, 255, 255)
    pdf.rect(15, col_split_y, 115, 18, style='DF', round_corners=True, corner_radius=3)
    
    pdf.set_font('helvetica', 'B', 6)
    pdf.set_text_color(148, 163, 184)
    pdf.set_xy(20, col_split_y + 4)
    pdf.cell(40, 3, "REPORTER")
    pdf.cell(40, 3, "ASSIGNEES")
    
    pdf.set_font('helvetica', 'B', 8)
    pdf.set_text_color(15, 23, 42)
    pdf.set_xy(20, col_split_y + 9)
    rep = safe_str(t.get('reporter', 'Unknown'))
    if len(rep)>15: rep = rep[:12]+"..."
    pdf.cell(40, 4, rep)
    
    ass = ", ".join(t.get('assignees', []))
    if not ass: ass = "Unassigned"
    if len(ass)>15: ass = ass[:12]+"..."
    pdf.cell(40, 4, safe_str(ass))
    
    # Right Column: Summary Card
    pdf.set_fill_color(14, 165, 233) # sky-500
    pdf.set_draw_color(14, 165, 233)
    pdf.rect(135, col_split_y, 60, 35, style='DF', round_corners=True, corner_radius=3)
    
    pdf.set_font('helvetica', 'B', 7)
    pdf.set_text_color(255, 255, 255)
    pdf.set_xy(140, col_split_y + 4)
    pdf.cell(50, 4, "TICKET SUMMARY")
    pdf.set_draw_color(56, 189, 248) # sky-400
    pdf.line(140, col_split_y + 9, 190, col_split_y + 9)
    
    pdf.set_font('helvetica', '', 7)
    pdf.set_xy(140, col_split_y + 11)
    pdf.cell(20, 5, "Status")
    if t_status: draw_pill(pdf, 175, col_split_y + 11.5, 15, 4, t_status, (220,252,231), (22,163,74))
    
    pdf.set_xy(140, col_split_y + 18)
    pdf.cell(20, 5, "Priority")
    if t_priority: draw_pill(pdf, 175, col_split_y + 18.5, 15, 4, t_priority, (254,242,242), (220,38,38))
    
    pdf.set_xy(140, col_split_y + 25)
    pdf.cell(20, 5, "Type")
    if t_type: draw_pill(pdf, 175, col_split_y + 25.5, 15, 4, t_type, (224,242,254), (2,132,199))
    
    # Left Column: Description
    pdf.set_y(col_split_y + 22)
    pdf.set_font('helvetica', 'B', 7)
    pdf.set_text_color(15, 23, 42)
    pdf.cell(115, 5, "DESCRIPTION", new_x=XPos.LMARGIN, new_y=YPos.NEXT)
    
    # Timeline
    pdf.set_xy(135, col_split_y + 40)
    pdf.set_font('helvetica', 'B', 7)
    pdf.set_text_color(15, 23, 42)
    pdf.cell(60, 5, "TIMELINE")
    
    return pdf

print("Syntax OK")
