<?php
// Start session and simulate login
session_start();
$_SESSION['user_id'] = 1; // Simulate admin user
$_SESSION['user_role'] = 'admin';
$_SESSION['username'] = 'admin';

// Include the marksheet logic
require_once 'config/database.php';
require_once 'includes/site_settings.php';

// Simulate GET parameters
$_GET['student_id'] = '6279';
$_GET['session'] = '2025';
$_GET['exam_type'] = 'final';

// Get site settings
$settings = get_all_settings($pdo);
$site_name = $settings['site_name'] ?? 'বিদ্যা নিকেতন';
$site_address = $settings['site_address'] ?? 'ঠিকানা: গ্রাম/থানা, জেলা, বাংলাদেশ';
$logo_image = $settings['logo_image'] ?? null;
$logo_size = isset($settings['logo_size']) ? intval($settings['logo_size']) : 100;

// Get search parameters
$student_id = $_GET['student_id'] ?? '';
$session = $_GET['session'] ?? date('Y');
$exam_type = $_GET['exam_type'] ?? '';

echo "Parameters: student_id=$student_id, session=$session, exam_type=$exam_type\n";

$student = null;
$results = [];

if ($student_id && $session && $exam_type) {
    echo "Processing student marksheet...\n";

    // Get student details
    $stmt = $pdo->prepare("
        SELECT s.*, c.name as class_name, c.section
        FROM students s
        JOIN classes c ON s.class_id = c.id
        WHERE s.id = ?
    ");
    $stmt->execute([$student_id]);
    $student = $stmt->fetch();

    if ($student) {
        echo "Student found: {$student['first_name']} {$student['last_name']}\n";

        // Get results for the student
        $stmt = $pdo->prepare("
            SELECT
                COALESCE(r.id, NULL) as result_id,
                COALESCE(r.marks_obtained, 0) as marks_obtained,
                COALESCE(r.grade, 'F') as grade,
                ss.subject_id,
                sub.name as subject_name,
                ss.is_fourth_subject
            FROM student_subjects ss
            LEFT JOIN subjects sub ON ss.subject_id = sub.id
            LEFT JOIN (
                SELECT *
                FROM results
                WHERE student_id = ? AND session = ? AND exam_type = ?
                AND id IN (
                    SELECT MAX(id)
                    FROM results
                    WHERE student_id = ? AND session = ? AND exam_type = ?
                    GROUP BY subject_id
                )
            ) r ON ss.student_id = r.student_id AND ss.subject_id = r.subject_id
            WHERE ss.student_id = ? AND sub.status = 'active'
            ORDER BY CASE WHEN ss.is_fourth_subject = 1 THEN 1 ELSE 0 END, sub.name
        ");
        $stmt->execute([$student_id, $session, $exam_type, $student_id, $session, $exam_type, $student_id]);
        $results = $stmt->fetchAll();

        echo "Results loaded: " . count($results) . " subjects\n";

        if (!empty($results)) {
            echo "Sample results:\n";
            foreach (array_slice($results, 0, 3) as $result) {
                echo "  - {$result['subject_name']} (Grade: {$result['grade']})\n";
            }
        }

        // Now check if the template rendering would work
        echo "\nTemplate rendering check:\n";
        echo "Student ID condition: " . ($student_id ? 'true' : 'false') . "\n";
        echo "Session condition: " . ($session ? 'true' : 'false') . "\n";
        echo "Exam type condition: " . ($exam_type ? 'true' : 'false') . "\n";
        echo "All conditions met: " . (($student_id && $session && $exam_type) ? 'YES' : 'NO') . "\n";

        if ($student_id && $session && $exam_type) {
            echo "Template should render marksheet\n";
        }

    } else {
        echo "Student not found!\n";
    }
} else {
    echo "Missing required parameters\n";
}
?>

