<?php
require_once __DIR__ . '/fpdf/fpdf.php';

function generateMarathonTicketPDF($name, $ticketId, $orderNumber, $orderDate) {
    $qrData = urlencode($ticketId.'|'.$name);
    $qrUrl  = "https://api.qrserver.com/v1/create-qr-code/?size=300x300&data={$qrData}";
    $qrTmp  = sys_get_temp_dir().'/qr_'.$ticketId.'.png';
    $qrImage = @file_get_contents($qrUrl);
    if ($qrImage!==false) file_put_contents($qrTmp, $qrImage);

    $pdf = new FPDF('P','mm','A4');
    $pdf->AddPage();
    $pdf->SetMargins(15,15,15);

    /* Header bar — dark gold */
    $pdf->SetFillColor(201,168,76);
    $pdf->Rect(0,0,210,8,'F');

    $pdf->SetXY(15,18);
    $pdf->SetFont('Arial','B',11);
    $pdf->SetTextColor(140,140,140);
    $pdf->Cell(0,6,'RECHARGE NATION',0,1);

    $pdf->SetXY(150,18);
    $pdf->SetFont('Arial','',9);
    $pdf->SetTextColor(120,120,120);
    $pdf->Cell(45,6,"Order #{$orderNumber}",0,1,'R');

    /* Card border */
    $cardY=28; $cardH=90;
    $pdf->SetDrawColor(201,168,76);
    $pdf->Rect(15,$cardY,180,$cardH);

    $pdf->SetXY(20,$cardY+8);
    $pdf->SetFont('Arial','B',22);
    $pdf->SetTextColor(20,20,20);
    $pdf->Cell(0,9,'MARATHON PRAYER HUB RETREAT',0,1);

    $pdf->SetXY(20,$cardY+18);
    $pdf->SetFont('Arial','',13);
    $pdf->SetTextColor(60,60,60);
    $pdf->Cell(0,7,'No More Boundaries — Isaiah 54:2',0,1);

    $pdf->SetXY(20,$cardY+30);
    $pdf->SetFont('Arial','B',10);
    $pdf->SetTextColor(40,40,40);
    $pdf->Cell(0,5,'114-126 Westmoor Street, Charlton, London SE7 8NQ',0,1);

    $pdf->SetXY(20,$cardY+36);
    $pdf->SetFont('Arial','B',10);
    $pdf->Cell(0,5,'Saturday, 1st August 2026 | 10:00 AM - 1:00 PM (UK Time)',0,1);

    $pdf->SetXY(20,$cardY+42);
    $pdf->SetFont('Arial','',10);
    $pdf->SetTextColor(80,80,80);
    $pdf->Cell(0,5,'Free Entry',0,1);

    $pdf->SetXY(20,$cardY+54);
    $pdf->SetFont('Arial','',8);
    $pdf->SetTextColor(150,150,150);
    $pdf->Cell(0,4,'Order Information',0,1);

    $pdf->SetXY(20,$cardY+59);
    $pdf->SetFont('Arial','',9);
    $pdf->SetTextColor(60,60,60);
    $pdf->MultiCell(110,5,"Order #{$orderNumber}. Registered by {$name} on {$orderDate}");

    /* Ticket ID */
    $pdf->SetXY(20,$cardY+78);
    $pdf->SetFont('Arial','',9);
    $pdf->SetTextColor(80,80,80);
    $pdf->Cell(22,6,'Ticket ID:',0,0);
    $pdf->SetFont('Arial','B',11);
    $pdf->SetTextColor(20,20,20);
    $pdf->Cell(0,6,$ticketId,0,1);

    if (file_exists($qrTmp)) {
        $pdf->Image($qrTmp,150,$cardY+8,35,35,'PNG');
        @unlink($qrTmp);
    }

    $pdf->SetXY(15,$cardY+$cardH+8);
    $pdf->SetFont('Arial','U',10);
    $pdf->SetTextColor(60,100,180);
    $pdf->Cell(0,6,'Bring this ticket (printed or on your phone) on the day',0,1);

    $pdf->SetXY(15,$cardY+$cardH+18);
    $pdf->SetFont('Arial','',9);
    $pdf->SetTextColor(140,140,140);
    $pdf->Cell(0,5,'Questions? Contact 07449157502',0,1);
    $pdf->SetX(15);
    $pdf->Cell(0,5,'Follow @aramidefadilepo and @rechargenation on Instagram',0,1);

    return $pdf->Output('S');
}
