<?php
require_once 'config/database.php';

try {
    // Get first student from class 10
    $stmt = $pdo->prepare('SELECT id, student_id, first_name, last_name FROM students WHERE class_id = ? AND status = "active" LIMIT 1');
    $stmt->execute([10]);
    $student = $stmt->fetch();

    if (!$student) {
        echo 'No student found' . PHP_EOL;
        exit;
    }

    echo 'Testing total marks calculation for student: ' . $student['student_id'] . ' - ' . $student['first_name'] . ' ' . $student['last_name'] . PHP_EOL . PHP_EOL;

    // Get student subjects
    $stmt = $pdo->prepare('
        SELECT ss.subject_id, s.name as subject_name, ss.is_fourth_subject
        FROM student_subjects ss
        JOIN subjects s ON ss.subject_id = s.id
        WHERE ss.student_id = ?
        ORDER BY s.name
    ');
    $stmt->execute([$student['id']]);
    $student_subjects = $stmt->fetchAll();

    echo 'Student subjects (' . count($student_subjects) . '):' . PHP_EOL;
    foreach ($student_subjects as $subject) {
        echo '  - ' . $subject['subject_name'] . ' (ID: ' . $subject['subject_id'] . ')' . PHP_EOL;
    }
    echo PHP_EOL;

    // Get results
    $stmt = $pdo->prepare('
        SELECT r.subject_id, r.marks_obtained, sub.name as subject_name
        FROM results r
        LEFT JOIN subjects sub ON r.subject_id = sub.id
        WHERE r.student_id = ? AND r.session = ? AND r.exam_type = ?
        ORDER BY sub.name
    ');
    $stmt->execute([$student['id'], '2025', 'test']);
    $results = $stmt->fetchAll();

    echo 'Results found (' . count($results) . '):' . PHP_EOL;
    foreach ($results as $result) {
        echo '  - ' . $result['subject_name'] . ': ' . $result['marks_obtained'] . ' marks' . PHP_EOL;
    }
    echo PHP_EOL;

    // Simulate the calculation from marksheet code
    $final_results = [];
    $total_marks = 0;
    $total_obtained = 0;

    foreach ($student_subjects as $subject) {
        $subject_id = $subject['subject_id'];

        // Check if student has marks for this subject
        $has_result = false;
        $marks_obtained = 0;

        foreach ($results as $result) {
            if ($result['subject_id'] == $subject_id) {
                $has_result = true;
                $marks_obtained = $result['marks_obtained'];
                break;
            }
        }

        if ($has_result) {
            // Get mark distribution
            $stmt = $pdo->prepare('
                SELECT total_marks, overall_pass_marks
                FROM subject_mark_distribution
                WHERE subject_id = ? AND class_id = ? AND session = ? AND exam_type = ?
                LIMIT 1
            ');
            $stmt->execute([$subject_id, 10, '2025', 'test']);
            $smd = $stmt->fetch();

            $full_marks = 100; // default
            if ($smd) {
                $full_marks = $smd['total_marks'];
            }

            $final_results[] = [
                'subject_name' => $subject['subject_name'],
                'full_marks' => $full_marks,
                'marks_obtained' => $marks_obtained
            ];

            $total_marks += $full_marks;
            $total_obtained += $marks_obtained;

            echo 'Subject: ' . $subject['subject_name'] . ' - Full marks: ' . $full_marks . ', Obtained: ' . $marks_obtained . PHP_EOL;
        } else {
            echo 'Subject: ' . $subject['subject_name'] . ' - NO RESULT FOUND' . PHP_EOL;
        }
    }

    echo PHP_EOL . 'FINAL CALCULATION:' . PHP_EOL;
    echo 'Total marks: ' . $total_marks . PHP_EOL;
    echo 'Total obtained: ' . $total_obtained . PHP_EOL;
    echo 'Ratio: ' . $total_obtained . '/' . $total_marks . PHP_EOL;

} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
?>
