<?php
require_once('includes/TCPDF-main/tcpdf.php');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    die('Invalid request.');
}

// --- Data Retrieval and Sanitization ---
$primary_style_name = isset($_POST['primary_style_name']) ? htmlspecialchars($_POST['primary_style_name']) : 'N/A';
$scores = isset($_POST['scores']) && is_array($_POST['scores']) ? $_POST['scores'] : [];
$tools = isset($_POST['tools']) && is_array($_POST['tools']) ? $_POST['tools'] : [];
$study_plan = isset($_POST['study_plan']) && is_array($_POST['study_plan']) ? $_POST['study_plan'] : [];
$stability_score = isset($_POST['stability_score']) ? htmlspecialchars($_POST['stability_score']) : 'N/A';
$stability_message = isset($_POST['stability_message']) ? htmlspecialchars($_POST['stability_message']) : 'No message available.';
$user_name = isset($_POST['user_name']) ? htmlspecialchars($_POST['user_name']) : 'N/A';

$styles_info = [
    'v' => ['name' => 'Visual'],
    'a' => ['name' => 'Auditory'],
    'r' => ['name' => 'Read/Write'],
    'k' => ['name' => 'Kinesthetic'],
];

// --- PDF Creation ---

class MYPDF extends TCPDF {
    public function Header() {
        $this->SetFont('helvetica', 'B', 20);
        $this->Cell(0, 15, 'RS Learning Lab - VARK Report', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        $this->Ln(5);
        $this->SetFont('helvetica', 'I', 8);
        $this->Cell(0, 10, 'Personalized Learning Style Assessment', 0, false, 'C', 0, '', 0, false, 'M', 'M');
        $this->Line(15, 30, 195, 30);
        $this->Image('assets/pasted-20251129-084541-8655b568.png', 165, 10, 30, 0, 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
    }

    public function Footer() {
        $this->SetY(-15);
        $this->SetFont('helvetica', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
        $this->Cell(0, 10, date('Y-m-d H:i:s'), 0, false, 'R', 0, '', 0, false, 'T', 'M');
    }
}

$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// Set document information
$pdf->SetCreator('RS Learning Lab');
$pdf->SetAuthor('RS Learning Lab');
$pdf->SetTitle('VARK Learning Style Report');
$pdf->SetSubject('Your Personalized Learning Assessment Results');

$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

$pdf->AddPage();

// --- Content ---
$pdf->SetFont('helvetica', '', 12);

// User Name
$pdf->SetFont('helvetica', 'B', 14);
$pdf->Cell(0, 15, 'Learning Style Report for: ' . $user_name, 0, 1, 'C');
$pdf->Ln(5);

// Primary Style
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 15, 'Your Primary Learning Style is:', 0, 1, 'C');
$pdf->SetFont('helvetica', 'B', 24);
$pdf->SetTextColor(0, 123, 255);
$pdf->Cell(0, 20, $primary_style_name, 0, 1, 'C');
$pdf->SetTextColor(0, 0, 0);
$pdf->Ln(10);

// Score Breakdown
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 15, 'Score Breakdown', 0, 1, 'L');
$pdf->SetFont('helvetica', '', 12);
$pdf->SetFillColor(248, 249, 250);

// Table Header
$pdf->SetFont('helvetica', 'B', 12);
$pdf->SetFillColor(0, 123, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(95, 10, 'Learning Style', 1, 0, 'C', 1);
$pdf->Cell(95, 10, 'Score (/20)', 1, 1, 'C', 1);

// Table Body
$pdf->SetFont('helvetica', '', 12);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetFillColor(248, 249, 250);
$fill = false;
foreach ($scores as $key => $score) {
    if (isset($styles_info[$key])) {
        $pdf->Cell(95, 10, $styles_info[$key]['name'], 1, 0, 'L', $fill);
        $pdf->Cell(95, 10, htmlspecialchars($score), 1, 1, 'C', $fill);
        $fill = !$fill;
    }
}

$pdf->Ln(10);

// Recommendations
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 15, 'Recommendations', 0, 1, 'L');
$pdf->SetFont('helvetica', '', 12);

$recommendations = [
    'Visual' => 'Seek out diagrams, charts, and videos. Use mind maps to connect concepts. Color-code your notes.',
    'Auditory' => 'Record lectures and listen back. Discuss topics with peers. Use mnemonic devices and rhymes.',
    'Read/Write' => 'Take detailed notes. Rewrite your notes. Read textbooks and articles. Create lists and outlines.',
    'Kinesthetic' => 'Learn by doing. Take breaks to move around. Use flashcards and hands-on models. Apply concepts to real-world scenarios.',
];

$pdf->MultiCell(0, 10, $recommendations[$primary_style_name] ?? 'No specific recommendations available.', 0, 'L');

$pdf->Ln(10);

// Recommended Tools
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 15, 'Recommended Tools', 0, 1, 'L');
$pdf->SetFont('helvetica', '', 12);

if (!empty($tools)) {
    foreach ($tools as $tool => $description) {
        $pdf->SetFont('helvetica', 'B', 12);
        $pdf->MultiCell(0, 10, '• ' . htmlspecialchars($tool), 0, 'L');
        $pdf->SetFont('helvetica', '', 12);
        $pdf->MultiCell(0, 10, '    ' . htmlspecialchars($description), 0, 'L');
        $pdf->Ln(2);
    }
} else {
    $pdf->Cell(0, 10, 'No specific tools recommended.', 0, 1, 'L');
}

$pdf->Ln(10);

// Personalized Study Time Allocation
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 15, 'Personalized Study Time Allocation (60-Min Model)', 0, 1, 'L');
$pdf->SetFont('helvetica', '', 12);

if (!empty($study_plan)) {
    // Table Header
    $pdf->SetFont('helvetica', 'B', 12);
    $pdf->SetFillColor(0, 123, 255);
    $pdf->SetTextColor(255, 255, 255);
    $pdf->Cell(95, 10, 'Activity', 1, 0, 'C', 1);
    $pdf->Cell(95, 10, 'Time', 1, 1, 'C', 1);

    // Table Body
    $pdf->SetFont('helvetica', '', 12);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->SetFillColor(248, 249, 250);
    $fill = false;
    foreach ($study_plan as $item) {
        $pdf->Cell(95, 10, htmlspecialchars($item['Activity']), 1, 0, 'L', $fill);
        $pdf->Cell(95, 10, htmlspecialchars($item['Time']), 1, 1, 'C', $fill);
        $fill = !$fill;
    }
} else {
    $pdf->Cell(0, 10, 'No study plan available.', 0, 1, 'L');
}


$pdf->Ln(10);

// Stability Score
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 15, 'Stability Score', 0, 1, 'L');
$pdf->SetFont('helvetica', '', 12);
$pdf->SetFont('helvetica', 'B', 20);
$pdf->Cell(0, 15, $stability_score . '/10', 0, 1, 'L');
$pdf->SetFont('helvetica', '', 12);
$pdf->MultiCell(0, 10, $stability_message, 0, 'L');



// --- Output ---
$pdf->Output('VARK_Report.pdf', 'I');

?>
