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

echo "<h1>🔍 Check Saved Marks</h1>";

// Check recent results based on the log
$subject_id = 2;
$class_id = 11;
$session = '2024';
$exam_type = 'midterm';

echo "<h2>1. Checking for Recent Marks</h2>";
echo "<p><strong>Looking for:</strong> Subject ID: $subject_id, Class ID: $class_id, Session: $session, Exam Type: $exam_type</p>";

try {
    $stmt = $pdo->prepare("SELECT * FROM results WHERE subject_id = ? AND session = ? AND exam_type = ? ORDER BY created_at DESC LIMIT 20");
    $stmt->execute([$subject_id, $session, $exam_type]);
    $results = $stmt->fetchAll();
    
    if (empty($results)) {
        echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
        echo "<p>❌ No results found for this combination!</p>";
        echo "</div>";
    } else {
        echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px;'>";
        echo "<p>✅ Found " . count($results) . " results!</p>";
        echo "</div>";
        
        echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
        echo "<tr style='background: #f8f9fa;'>";
        echo "<th>ID</th><th>Student ID</th><th>CQ</th><th>MCQ</th><th>Practical</th><th>Assignment</th><th>Attendance</th><th>Total</th><th>Grade</th><th>Created At</th>";
        echo "</tr>";
        
        foreach ($results as $result) {
            echo "<tr>";
            echo "<td>{$result['id']}</td>";
            echo "<td>{$result['student_id']}</td>";
            echo "<td>{$result['cq_marks']}</td>";
            echo "<td>{$result['mcq_marks']}</td>";
            echo "<td>{$result['practical_marks']}</td>";
            echo "<td>{$result['assignment_marks']}</td>";
            echo "<td>{$result['attendance_marks']}</td>";
            echo "<td><strong>{$result['marks_obtained']}</strong></td>";
            echo "<td><strong>{$result['grade']}</strong></td>";
            echo "<td>{$result['created_at']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
    echo "</div>";
}

// Check all recent results
echo "<h2>2. All Recent Results (Last 10)</h2>";
try {
    $stmt = $pdo->query("SELECT * FROM results ORDER BY created_at DESC LIMIT 10");
    $all_results = $stmt->fetchAll();
    
    if (empty($all_results)) {
        echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
        echo "<p>❌ No results found in database at all!</p>";
        echo "</div>";
    } else {
        echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
        echo "<tr style='background: #f8f9fa;'>";
        echo "<th>ID</th><th>Student ID</th><th>Subject ID</th><th>Session</th><th>Exam Type</th><th>Total</th><th>Grade</th><th>Created At</th>";
        echo "</tr>";
        
        foreach ($all_results as $result) {
            echo "<tr>";
            echo "<td>{$result['id']}</td>";
            echo "<td>{$result['student_id']}</td>";
            echo "<td>{$result['subject_id']}</td>";
            echo "<td>{$result['session']}</td>";
            echo "<td>{$result['exam_type']}</td>";
            echo "<td>{$result['marks_obtained']}</td>";
            echo "<td>{$result['grade']}</td>";
            echo "<td>{$result['created_at']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
    echo "</div>";
}

// Check specific student from log (1647)
echo "<h2>3. Check Specific Student (1647)</h2>";
try {
    $stmt = $pdo->prepare("SELECT * FROM results WHERE student_id = 1647 ORDER BY created_at DESC LIMIT 5");
    $stmt->execute();
    $student_results = $stmt->fetchAll();
    
    if (empty($student_results)) {
        echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
        echo "<p>❌ No results found for student 1647!</p>";
        echo "</div>";
    } else {
        echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px;'>";
        echo "<p>✅ Found " . count($student_results) . " results for student 1647!</p>";
        echo "</div>";
        
        echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
        echo "<tr style='background: #f8f9fa;'>";
        echo "<th>ID</th><th>Subject ID</th><th>Session</th><th>Exam Type</th><th>CQ</th><th>MCQ</th><th>Total</th><th>Grade</th><th>Created At</th>";
        echo "</tr>";
        
        foreach ($student_results as $result) {
            echo "<tr>";
            echo "<td>{$result['id']}</td>";
            echo "<td>{$result['subject_id']}</td>";
            echo "<td>{$result['session']}</td>";
            echo "<td>{$result['exam_type']}</td>";
            echo "<td>{$result['cq_marks']}</td>";
            echo "<td>{$result['mcq_marks']}</td>";
            echo "<td><strong>{$result['marks_obtained']}</strong></td>";
            echo "<td><strong>{$result['grade']}</strong></td>";
            echo "<td>{$result['created_at']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
    echo "</div>";
}

// Check mark distributions
echo "<h2>4. Check Mark Distributions</h2>";
try {
    $stmt = $pdo->query("SELECT * FROM mark_distributions ORDER BY created_at DESC LIMIT 5");
    $distributions = $stmt->fetchAll();
    
    echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
    echo "<tr style='background: #f8f9fa;'>";
    echo "<th>ID</th><th>Subject ID</th><th>Class ID</th><th>Session</th><th>Exam Type</th><th>Total Marks</th><th>Created At</th>";
    echo "</tr>";
    
    foreach ($distributions as $dist) {
        echo "<tr>";
        echo "<td>{$dist['id']}</td>";
        echo "<td>{$dist['subject_id']}</td>";
        echo "<td>{$dist['class_id']}</td>";
        echo "<td>{$dist['session']}</td>";
        echo "<td>{$dist['exam_type']}</td>";
        echo "<td>{$dist['total_marks']}</td>";
        echo "<td>{$dist['created_at']}</td>";
        echo "</tr>";
    }
    echo "</table>";
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
    echo "</div>";
}

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