<?php
// Start a session
session_start();

// Set dummy session for testing
$_SESSION['user_id'] = 1;
$_SESSION['username'] = 'admin';
$_SESSION['user_role'] = 'admin';
$_SESSION['user_email'] = 'admin@example.com';
$_SESSION['user_name'] = 'Admin';

echo "Set dummy session for testing\n";

// Now access the marksheet page
$url = 'http://localhost/bidda/reports/all_subjects_marksheet.php?student_id=6279&session=2025&exam_type=final';

// Use file_get_contents instead of curl to maintain session
$context = stream_context_create([
    'http' => [
        'header' => 'Cookie: PHPSESSID=' . session_id() . "\r\n",
    ]
]);

$output = file_get_contents($url, false, $context);

// Check for PHP errors
if (strpos($output, 'Parse error') !== false || strpos($output, 'Fatal error') !== false) {
    echo "PHP Error found in output:\n";
    // Extract error message
    preg_match('/(Parse error|Fatal error).*?on line \d+/s', $output, $error_match);
    if ($error_match) {
        echo $error_match[0] . "\n";
    }
    echo "First 1000 characters:\n" . substr($output, 0, 1000) . "\n";
    exit;
}

// Extract GPA from the output
preg_match('/Grade Point Average.*?:.*?([0-9.]+)/s', $output, $matches);
if (isset($matches[1])) {
    echo 'GPA: ' . $matches[1] . PHP_EOL;
} else {
    echo 'GPA not found in output' . PHP_EOL;
}

// Extract grades from combined subjects
preg_match_all('/বাংলা\(একত্রে\).*?([A-F][+-]?)/s', $output, $bangla_matches);
preg_match_all('/ইংরেজি\(একত্রে\).*?([A-F][+-]?)/s', $output, $english_matches);

echo 'Bangla combined grades: ' . implode(', ', $bangla_matches[1] ?? []) . PHP_EOL;
echo 'English combined grades: ' . implode(', ', $english_matches[1] ?? []) . PHP_EOL;

// Show first 1000 characters to see what's being output
echo "\nFirst 1000 characters of output:\n";
echo substr($output, 0, 1000) . "\n";