<?php
// Minimal PDF Test for Report System
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Report System PDF Test</h2>";

// Include necessary files
if (file_exists('config.php')) {
    require_once 'config.php';
    echo "<p style='color: green;'>✅ Config loaded</p>";
} else {
    echo "<p style='color: red;'>❌ Config not found</p>";
    exit;
}

if (file_exists('vendor/autoload.php')) {
    require_once 'vendor/autoload.php';
    echo "<p style='color: green;'>✅ Dompdf autoload loaded</p>";
} else {
    echo "<p style='color: red;'>❌ Dompdf autoload not found</p>";
    exit;
}

// Check database connection
if (isset($connection) && $connection instanceof mysqli) {
    if ($connection->connect_error) {
        echo "<p style='color: red;'>❌ Database connection failed: " . $connection->connect_error . "</p>";
        exit;
    }
    echo "<p style='color: green;'>✅ Database connection OK</p>";
    
    // Test getting some basic data
    $result = $connection->query("SELECT class_id, class_name FROM classes LIMIT 5");
    if ($result && $result->num_rows > 0) {
        echo "<p style='color: green;'>✅ Found " . $result->num_rows . " classes</p>";
        
        // Get the first class
        $class = $result->fetch_assoc();
        $test_class_id = $class['class_id'];
        $test_class_name = $class['class_name'];
        echo "<p>Testing with Class: ID=$test_class_id, Name=$test_class_name</p>";
        
        // Try to get exams for this class
        $exam_result = $connection->query("SELECT DISTINCT exam_id, exam_name FROM exams LIMIT 1");
        if ($exam_result && $exam_result->num_rows > 0) {
            $exam = $exam_result->fetch_assoc();
            $test_exam_id = $exam['exam_id'];
            $test_exam_name = $exam['exam_name'];
            echo "<p>Testing with Exam: ID=$test_exam_id, Name=$test_exam_name</p>";
            
            // Test PDF generation with basic data
            try {
                $dompdf = new Dompdf\Dompdf();
                $html = '
                <h1 style="color: blue;">School Report Card</h1>
                <h2>' . htmlspecialchars($test_class_name) . '</h2>
                <h3>Exam: ' . htmlspecialchars($test_exam_name) . '</h3>
                <p><strong>Generated:</strong> ' . date('Y-m-d H:i:s') . '</p>
                <p>This is a test report card to verify PDF generation is working.</p>
                <table border="1" style="border-collapse: collapse; width: 100%;">
                    <tr style="background-color: #f0f0f0;">
                        <th>Student Name</th>
                        <th>Mathematics</th>
                        <th>English</th>
                        <th>Total</th>
                    </tr>
                    <tr>
                        <td>Test Student 1</td>
                        <td>85</td>
                        <td>90</td>
                        <td>175</td>
                    </tr>
                    <tr>
                        <td>Test Student 2</td>
                        <td>78</td>
                        <td>82</td>
                        <td>160</td>
                    </tr>
                </table>';
                
                $dompdf->loadHtml($html);
                $dompdf->setPaper('A4', 'portrait');
                $dompdf->render();
                $output = $dompdf->output();
                
                echo "<p style='color: green;'>✅ Report PDF generated successfully</p>";
                echo "<p>PDF size: " . strlen($output) . " bytes</p>";
                
                // Save test PDF
                file_put_contents('report_test.pdf', $output);
                echo "<p style='color: blue;'>📄 Test report saved as 'report_test.pdf'</p>";
                
                // Download link
                echo "<p><a href='report_test.pdf' download>📥 Download Test Report</a></p>";
                
            } catch (Exception $e) {
                echo "<p style='color: red;'>❌ PDF generation failed: " . $e->getMessage() . "</p>";
                echo "<p>Stack trace:</p>";
                echo "<pre>" . $e->getTraceAsString() . "</pre>";
            }
            
        } else {
            echo "<p style='color: orange;'>⚠️ No exams found in database</p>";
        }
        
    } else {
        echo "<p style='color: orange;'>⚠️ No classes found in database</p>";
    }
    
} else {
    echo "<p style='color: red;'>❌ Database connection object not found</p>";
}
?>