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

echo "<h1>🔍 Check Mark Distribution</h1>";

// Check the mark distribution for Subject 2, Class 11, Session 2024, Midterm
$subject_id = 2;
$class_id = 11;
$session = '2024';
$exam_type = 'midterm';

try {
    $stmt = $pdo->prepare("SELECT * FROM mark_distributions WHERE subject_id = ? AND class_id = ? AND session = ? AND exam_type = ?");
    $stmt->execute([$subject_id, $class_id, $session, $exam_type]);
    $mark_distribution = $stmt->fetch();
    
    if ($mark_distribution) {
        echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px;'>";
        echo "<h2>✅ Mark Distribution Found:</h2>";
        echo "<ul>";
        echo "<li><strong>ID:</strong> {$mark_distribution['id']}</li>";
        echo "<li><strong>CQ Marks:</strong> {$mark_distribution['cq_marks']}</li>";
        echo "<li><strong>MCQ Marks:</strong> {$mark_distribution['mcq_marks']}</li>";
        echo "<li><strong>Practical Marks:</strong> {$mark_distribution['practical_marks']}</li>";
        echo "<li><strong>Assignment Marks:</strong> {$mark_distribution['assignment_marks']}</li>";
        echo "<li><strong>Attendance Marks:</strong> {$mark_distribution['attendance_marks']}</li>";
        echo "<li><strong>Total Marks:</strong> {$mark_distribution['total_marks']}</li>";
        echo "<li><strong>Group Name:</strong> '{$mark_distribution['group_name']}'</li>";
        echo "<li><strong>Created At:</strong> {$mark_distribution['created_at']}</li>";
        echo "</ul>";
        echo "</div>";
        
        // Show the validation logic
        echo "<h2>🔧 Validation Logic:</h2>";
        echo "<div style='background: #e7f3ff; padding: 15px; border-radius: 5px;'>";
        echo "<p><strong>Current validation:</strong></p>";
        echo "<ul>";
        echo "<li>CQ: max(0, min(entered_value, {$mark_distribution['cq_marks']}))</li>";
        echo "<li>MCQ: max(0, min(entered_value, {$mark_distribution['mcq_marks']}))</li>";
        echo "<li>Practical: max(0, min(entered_value, {$mark_distribution['practical_marks']}))</li>";
        echo "<li>Assignment: max(0, min(entered_value, {$mark_distribution['assignment_marks']}))</li>";
        echo "<li>Attendance: max(0, min(entered_value, {$mark_distribution['attendance_marks']}))</li>";
        echo "</ul>";
        echo "<p><strong>Example:</strong> If you enter CQ = 50, it will be capped to {$mark_distribution['cq_marks']}</p>";
        echo "</div>";
        
    } else {
        echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
        echo "<p>❌ No mark distribution found for this combination!</p>";
        echo "</div>";
    }
    
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
    echo "</div>";
}

// Show all mark distributions for reference
echo "<h2>📋 All Mark Distributions:</h2>";
try {
    $stmt = $pdo->query("SELECT * FROM mark_distributions ORDER BY created_at DESC LIMIT 10");
    $all_distributions = $stmt->fetchAll();
    
    if (!empty($all_distributions)) {
        echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
        echo "<tr style='background: #f8f9fa;'>";
        echo "<th>ID</th><th>Subject</th><th>Class</th><th>Session</th><th>Exam Type</th><th>CQ</th><th>MCQ</th><th>Total</th><th>Created</th>";
        echo "</tr>";
        
        foreach ($all_distributions as $dist) {
            $highlight = ($dist['subject_id'] == $subject_id && $dist['class_id'] == $class_id && $dist['session'] == $session && $dist['exam_type'] == $exam_type) ? 'background: #fff3cd;' : '';
            echo "<tr style='$highlight'>";
            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><strong>{$dist['cq_marks']}</strong></td>";
            echo "<td><strong>{$dist['mcq_marks']}</strong></td>";
            echo "<td><strong>{$dist['total_marks']}</strong></td>";
            echo "<td>{$dist['created_at']}</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>";
?>