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

echo "<h1>🔍 Test Marks Display</h1>";

// Test the exact query that's used in entry.php
$subject_id = 2;
$class_id = 11;
$session = '2024';
$exam_type = 'midterm';
$actual_group_name = '';

echo "<h2>Testing Query with:</h2>";
echo "<ul>";
echo "<li>Subject ID: $subject_id</li>";
echo "<li>Class ID: $class_id</li>";
echo "<li>Session: $session</li>";
echo "<li>Exam Type: $exam_type</li>";
echo "<li>Group Name: '$actual_group_name'</li>";
echo "</ul>";

// Build the same query as in entry.php
if (empty($actual_group_name) || $actual_group_name == '') {
    $whereGroup = "AND (s.group_name IS NULL OR s.group_name = '')";
    $resultsGroupCondition = "AND (r.group_name IS NULL OR r.group_name = '')";
    $executeParams = [$subject_id, $session, $exam_type, $class_id, $session];
} else {
    $whereGroup = "AND s.group_name = ?";
    $resultsGroupCondition = "AND r.group_name = ?";
    $executeParams = [$subject_id, $session, $actual_group_name, $exam_type, $class_id, $session, $actual_group_name];
}

$query = "
    SELECT s.id, s.student_id, s.first_name, s.last_name, s.roll_number,
           r.id as result_id, r.marks_obtained, r.cq_marks, r.mcq_marks, 
           r.practical_marks, r.assignment_marks, r.attendance_marks, r.grade
    FROM students s
    LEFT JOIN results r ON s.id = r.student_id 
        AND r.subject_id = ? AND r.exam_id IS NULL AND r.session = ? 
        $resultsGroupCondition AND r.exam_type = ?
    WHERE s.class_id = ? AND s.session = ? $whereGroup AND s.status = 'active'
    ORDER BY s.id, s.roll_number, s.first_name
    LIMIT 10
";

echo "<h2>Query:</h2>";
echo "<pre>" . htmlspecialchars($query) . "</pre>";

echo "<h2>Parameters:</h2>";
echo "<pre>" . print_r($executeParams, true) . "</pre>";

try {
    $stmt = $pdo->prepare($query);
    $stmt->execute($executeParams);
    $students = $stmt->fetchAll();
    
    echo "<h2>Results:</h2>";
    if (empty($students)) {
        echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
        echo "<p>❌ No students found!</p>";
        echo "</div>";
    } else {
        echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px;'>";
        echo "<p>✅ Found " . count($students) . " students</p>";
        echo "</div>";
        
        echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
        echo "<tr style='background: #f8f9fa;'>";
        echo "<th>Student ID</th><th>Name</th><th>Roll</th><th>Result ID</th><th>CQ</th><th>MCQ</th><th>Total</th><th>Grade</th>";
        echo "</tr>";
        
        foreach ($students as $student) {
            echo "<tr>";
            echo "<td>{$student['id']}</td>";
            echo "<td>{$student['first_name']} {$student['last_name']}</td>";
            echo "<td>{$student['roll_number']}</td>";
            echo "<td>" . ($student['result_id'] ?? 'NULL') . "</td>";
            echo "<td>" . ($student['cq_marks'] ?? '0.0') . "</td>";
            echo "<td>" . ($student['mcq_marks'] ?? '0.0') . "</td>";
            echo "<td>" . ($student['marks_obtained'] ?? '0.0') . "</td>";
            echo "<td>" . ($student['grade'] ?? 'N/A') . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p>❌ Query Error: " . $e->getMessage() . "</p>";
    echo "</div>";
}

// Also check what's actually in results table
echo "<h2>Direct Results Check:</h2>";
try {
    $stmt = $pdo->prepare("SELECT * FROM results WHERE subject_id = ? AND session = ? AND exam_type = ? AND marks_obtained > 0");
    $stmt->execute([$subject_id, $session, $exam_type]);
    $direct_results = $stmt->fetchAll();
    
    if (empty($direct_results)) {
        echo "<p>❌ No results found in database</p>";
    } else {
        echo "<p>✅ Found " . count($direct_results) . " results in database:</p>";
        echo "<table border='1' style='border-collapse: collapse;'>";
        echo "<tr><th>ID</th><th>Student ID</th><th>Group Name</th><th>CQ</th><th>MCQ</th><th>Total</th><th>Grade</th></tr>";
        foreach ($direct_results as $result) {
            echo "<tr>";
            echo "<td>{$result['id']}</td>";
            echo "<td>{$result['student_id']}</td>";
            echo "<td>'" . ($result['group_name'] ?? '') . "'</td>";
            echo "<td>{$result['cq_marks']}</td>";
            echo "<td>{$result['mcq_marks']}</td>";
            echo "<td>{$result['marks_obtained']}</td>";
            echo "<td>{$result['grade']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
} catch (Exception $e) {
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
}

echo "<p><a href='marks/entry.php'>← Back to Marks Entry</a></p>";
?>