<?php
/**
 * PDF Generation Diagnostic Script
 * Run this to identify exactly what's causing the "Failed to load PDF document" error
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<!DOCTYPE html><html><head><title>PDF Diagnostic</title></head><body>";
echo "<h1>PDF Generation Diagnostic</h1>";

// Step 1: Check config loading
echo "<h2>1. Configuration Check</h2>";
try {
    include_once 'config.php';
    echo "✅ Config loaded successfully<br>";
    
    if (isset($conn)) {
        echo "✅ \$conn variable exists<br>";
        if ($conn->ping()) {
            echo "✅ Database connection is working<br>";
            echo "Database: " . DB_NAME . "<br>";
        } else {
            echo "❌ Database connection failed<br>";
        }
    } else {
        echo "❌ \$conn variable not found<br>";
    }
    
    if (isset($connection)) {
        echo "✅ \$connection variable exists (compatibility)<br>";
    } else {
        echo "⚠️ \$connection variable not found (will be created)<br>";
        $connection = $conn;
    }
    
} catch (Exception $e) {
    echo "❌ Config loading failed: " . $e->getMessage() . "<br>";
}

// Step 2: Check Dompdf
echo "<h2>2. Dompdf Check</h2>";
$dompdf_paths = [
    'vendor/autoload.php',
    'dompdf/autoload.inc.php',
    'lib/dompdf/autoload.inc.php'
];

$dompdf_loaded = false;
foreach ($dompdf_paths as $path) {
    if (file_exists($path)) {
        try {
            require_once $path;
            if (class_exists('Dompdf\Dompdf')) {
                echo "✅ Dompdf loaded from: $path<br>";
                $dompdf_loaded = true;
                break;
            }
        } catch (Exception $e) {
            echo "⚠️ Error loading Dompdf from $path: " . $e->getMessage() . "<br>";
        }
    }
}

if (!$dompdf_loaded) {
    echo "❌ Dompdf not found or not loaded<br>";
    echo "Install with: composer require dompdf/dompdf --ignore-platform-reqs<br>";
} else {
    echo "✅ Dompdf class available<br>";
}

// Step 3: Test basic PDF generation
echo "<h2>3. Basic PDF Generation Test</h2>";
if ($dompdf_loaded) {
    try {
        // Create a simple test PDF
        $html = '<html><body><h1>Test PDF</h1><p>This is a test PDF to verify Dompdf works.</p></body></html>';
        
        $dompdf = new \Dompdf\Dompdf();
        $dompdf->loadHtml($html);
        $dompdf->setPaper('A4', 'portrait');
        $dompdf->render();
        
        $pdf_content = $dompdf->output();
        echo "✅ Basic PDF generation works<br>";
        echo "PDF size: " . strlen($pdf_content) . " bytes<br>";
        
        // Save test file
        file_put_contents('test_pdf_basic.pdf', $pdf_content);
        echo "✅ Test PDF saved as test_pdf_basic.pdf<br>";
        
    } catch (Exception $e) {
        echo "❌ PDF generation failed: " . $e->getMessage() . "<br>";
    }
}

// Step 4: Check admin file syntax
echo "<h2>4. Admin File Syntax Check</h2>";
$admin_file = 'admin_exam_grade_reports.php';
if (file_exists($admin_file)) {
    $syntax_check = shell_exec("php -l " . escapeshellarg($admin_file) . " 2>&1");
    echo "PHP Syntax Check: $syntax_check<br>";
    
    // Check if admin file includes config
    $admin_content = file_get_contents($admin_file);
    if (strpos($admin_content, "include_once 'config.php'") !== false) {
        echo "✅ Admin file includes config.php<br>";
    } else {
        echo "❌ Admin file doesn't include config.php<br>";
    }
    
    if (strpos($admin_content, "\$connection = \$conn") !== false) {
        echo "✅ Admin file has compatibility fix<br>";
    } else {
        echo "⚠️ Admin file might not have compatibility fix<br>";
    }
} else {
    echo "❌ Admin file not found: $admin_file<br>";
}

// Step 5: Check session
echo "<h2>5. Session Check</h2>";
session_start();
echo "Session ID: " . session_id() . "<br>";
echo "Session data: <pre>";
print_r($_SESSION);
echo "</pre>";

echo "<h2>6. Recommendations</h2>";
echo "<ul>";

if (!$dompdf_loaded) {
    echo "<li>Install Dompdf: <code>composer require dompdf/dompdf --ignore-platform-reqs</code></li>";
}

echo "<li>If all checks pass, try manually testing the admin file by adding this at the top:</li>";
echo "<li><code>&lt;?php error_reporting(E_ALL); ini_set('display_errors', 1); ?&gt;</code></li>";

echo "</ul>";

echo "</body></html>";
?>