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

// Test individual passing marks functionality
echo "<!DOCTYPE html>
<html lang='bn'>
<head>
    <meta charset='UTF-8'>
    <title>Individual Passing Marks Test</title>
    <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>
</head>
<body class='bg-light'>
    <div class='container mt-4'>
        <div class='card'>
            <div class='card-header bg-primary text-white'>
                <h4>Individual Passing Marks Test</h4>
            </div>
            <div class='card-body'>";

// Include the calculateGrade function from entry.php
function calculateGrade($obtained_marks, $total_marks, $mark_distribution = null, $individual_marks = []) {
    if ($total_marks == 0) return 'F';
    
    // Check individual passing marks if set
    if ($mark_distribution) {
        // Check CQ passing marks
        if (!empty($mark_distribution['cq_pass_marks']) && $mark_distribution['cq_marks'] > 0) {
            $cq_obtained = (float)($individual_marks['cq'] ?? 0);
            if ($cq_obtained < $mark_distribution['cq_pass_marks']) {
                return 'F'; // Failed in CQ
            }
        }
        
        // Check MCQ passing marks
        if (!empty($mark_distribution['mcq_pass_marks']) && $mark_distribution['mcq_marks'] > 0) {
            $mcq_obtained = (float)($individual_marks['mcq'] ?? 0);
            if ($mcq_obtained < $mark_distribution['mcq_pass_marks']) {
                return 'F'; // Failed in MCQ
            }
        }
        
        // Check Practical passing marks
        if (!empty($mark_distribution['practical_pass_marks']) && $mark_distribution['practical_marks'] > 0) {
            $practical_obtained = (float)($individual_marks['practical'] ?? 0);
            if ($practical_obtained < $mark_distribution['practical_pass_marks']) {
                return 'F'; // Failed in Practical
            }
        }
        
        // Check overall passing marks
        $overall_pass_marks = $mark_distribution['overall_pass_marks'] ?? $mark_distribution['pass_marks'] ?? ($total_marks * 0.4);
        if ($obtained_marks < $overall_pass_marks) {
            return 'F'; // Failed overall
        }
    }
    
    $percentage = ($obtained_marks / $total_marks) * 100;
    
    if ($percentage >= 80) return 'A+';
    elseif ($percentage >= 70) return 'A';
    elseif ($percentage >= 60) return 'A-';
    elseif ($percentage >= 50) return 'B';
    elseif ($percentage >= 40) return 'C';
    elseif ($percentage >= 33) return 'D';
    else return 'F';
}

try {
    // Test case 1: Student passes overall but fails in CQ
    echo "<h5>Test Case 1: Student passes overall but fails in CQ</h5>";
    
    $mark_distribution = [
        'total_marks' => 100,
        'cq_marks' => 40,
        'mcq_marks' => 25,
        'practical_marks' => 20,
        'assignment_marks' => 10,
        'attendance_marks' => 5,
        'overall_pass_marks' => 40,
        'cq_pass_marks' => 16,  // CQ passing marks = 40% of 40 = 16
        'mcq_pass_marks' => 10, // MCQ passing marks = 40% of 25 = 10
        'practical_pass_marks' => 8 // Practical passing marks = 40% of 20 = 8
    ];
    
    $individual_marks = [
        'cq' => 10,        // Failed in CQ (less than 16)
        'mcq' => 20,       // Passed in MCQ
        'practical' => 15, // Passed in Practical
        'assignment' => 8,
        'attendance' => 5
    ];
    
    $total_obtained = 58; // Total is 58 (above 40 overall pass marks)
    $grade = calculateGrade($total_obtained, $mark_distribution['total_marks'], $mark_distribution, $individual_marks);
    
    echo "<div class='alert alert-info'>";
    echo "<strong>Mark Distribution:</strong><br>";
    echo "CQ: {$mark_distribution['cq_marks']} (Pass: {$mark_distribution['cq_pass_marks']})<br>";
    echo "MCQ: {$mark_distribution['mcq_marks']} (Pass: {$mark_distribution['mcq_pass_marks']})<br>";
    echo "Practical: {$mark_distribution['practical_marks']} (Pass: {$mark_distribution['practical_pass_marks']})<br>";
    echo "Overall Pass: {$mark_distribution['overall_pass_marks']}<br><br>";
    
    echo "<strong>Student Marks:</strong><br>";
    echo "CQ: {$individual_marks['cq']} (Required: {$mark_distribution['cq_pass_marks']})<br>";
    echo "MCQ: {$individual_marks['mcq']} (Required: {$mark_distribution['mcq_pass_marks']})<br>";
    echo "Practical: {$individual_marks['practical']} (Required: {$mark_distribution['practical_pass_marks']})<br>";
    echo "Total: $total_obtained (Required: {$mark_distribution['overall_pass_marks']})<br><br>";
    
    echo "<strong>Result: Grade = $grade</strong><br>";
    echo ($grade == 'F') ? "<span class='text-success'>✓ Correct! Student failed because CQ marks (10) < CQ pass marks (16)</span>" : "<span class='text-danger'>✗ Wrong! Should be F</span>";
    echo "</div>";
    
    // Test case 2: Student passes in all components
    echo "<h5>Test Case 2: Student passes in all components</h5>";
    
    $individual_marks2 = [
        'cq' => 25,        // Passed in CQ
        'mcq' => 20,       // Passed in MCQ
        'practical' => 15, // Passed in Practical
        'assignment' => 8,
        'attendance' => 5
    ];
    
    $total_obtained2 = 73;
    $grade2 = calculateGrade($total_obtained2, $mark_distribution['total_marks'], $mark_distribution, $individual_marks2);
    
    echo "<div class='alert alert-info'>";
    echo "<strong>Student Marks:</strong><br>";
    echo "CQ: {$individual_marks2['cq']} (Required: {$mark_distribution['cq_pass_marks']})<br>";
    echo "MCQ: {$individual_marks2['mcq']} (Required: {$mark_distribution['mcq_pass_marks']})<br>";
    echo "Practical: {$individual_marks2['practical']} (Required: {$mark_distribution['practical_pass_marks']})<br>";
    echo "Total: $total_obtained2 (Required: {$mark_distribution['overall_pass_marks']})<br><br>";
    
    echo "<strong>Result: Grade = $grade2</strong><br>";
    echo ($grade2 != 'F') ? "<span class='text-success'>✓ Correct! Student passed in all components</span>" : "<span class='text-danger'>✗ Wrong! Should not be F</span>";
    echo "</div>";
    
    // Test case 3: Check database for existing distributions with individual passing marks
    echo "<h5>Test Case 3: Database Check - Existing Distributions with Individual Passing Marks</h5>";
    
    $stmt = $pdo->query("
        SELECT smd.*, s.name as subject_name, c.name as class_name 
        FROM subject_mark_distribution smd
        JOIN subjects s ON smd.subject_id = s.id
        JOIN classes c ON smd.class_id = c.id
        WHERE (smd.cq_pass_marks IS NOT NULL OR smd.mcq_pass_marks IS NOT NULL OR smd.practical_pass_marks IS NOT NULL)
        LIMIT 5
    ");
    
    $distributions = $stmt->fetchAll();
    
    if (count($distributions) > 0) {
        echo "<div class='table-responsive'>";
        echo "<table class='table table-striped'>";
        echo "<thead><tr><th>Subject</th><th>Class</th><th>Exam Type</th><th>CQ Pass</th><th>MCQ Pass</th><th>Practical Pass</th><th>Overall Pass</th></tr></thead>";
        echo "<tbody>";
        foreach ($distributions as $dist) {
            echo "<tr>";
            echo "<td>{$dist['subject_name']}</td>";
            echo "<td>{$dist['class_name']}</td>";
            echo "<td>{$dist['exam_type']}</td>";
            echo "<td>" . ($dist['cq_pass_marks'] ?? 'N/A') . "</td>";
            echo "<td>" . ($dist['mcq_pass_marks'] ?? 'N/A') . "</td>";
            echo "<td>" . ($dist['practical_pass_marks'] ?? 'N/A') . "</td>";
            echo "<td>" . ($dist['overall_pass_marks'] ?? 'N/A') . "</td>";
            echo "</tr>";
        }
        echo "</tbody></table>";
        echo "</div>";
    } else {
        echo "<div class='alert alert-warning'>";
        echo "No distributions found with individual passing marks set. You may need to set up individual passing marks in the setup page.";
        echo "</div>";
    }
    
    // Test case 4: Check mark_distributions table as well
    echo "<h5>Test Case 4: Database Check - mark_distributions table</h5>";
    
    $stmt2 = $pdo->query("
        SELECT md.*, s.name as subject_name, c.name as class_name 
        FROM mark_distributions md
        JOIN subjects s ON md.subject_id = s.id
        JOIN classes c ON md.class_id = c.id
        WHERE (md.cq_pass_marks IS NOT NULL OR md.mcq_pass_marks IS NOT NULL OR md.practical_pass_marks IS NOT NULL)
        LIMIT 5
    ");
    
    $distributions2 = $stmt2->fetchAll();
    
    if (count($distributions2) > 0) {
        echo "<div class='table-responsive'>";
        echo "<table class='table table-striped'>";
        echo "<thead><tr><th>Subject</th><th>Class</th><th>Exam Type</th><th>CQ Pass</th><th>MCQ Pass</th><th>Practical Pass</th><th>Overall Pass</th></tr></thead>";
        echo "<tbody>";
        foreach ($distributions2 as $dist) {
            echo "<tr>";
            echo "<td>{$dist['subject_name']}</td>";
            echo "<td>{$dist['class_name']}</td>";
            echo "<td>{$dist['exam_type']}</td>";
            echo "<td>" . ($dist['cq_pass_marks'] ?? 'N/A') . "</td>";
            echo "<td>" . ($dist['mcq_pass_marks'] ?? 'N/A') . "</td>";
            echo "<td>" . ($dist['practical_pass_marks'] ?? 'N/A') . "</td>";
            echo "<td>" . ($dist['overall_pass_marks'] ?? 'N/A') . "</td>";
            echo "</tr>";
        }
        echo "</tbody></table>";
        echo "</div>";
    } else {
        echo "<div class='alert alert-warning'>";
        echo "No distributions found in mark_distributions table with individual passing marks set.";
        echo "</div>";
    }
    
} catch (Exception $e) {
    echo "<div class='alert alert-danger'>";
    echo "Error: " . $e->getMessage();
    echo "</div>";
}

echo "            </div>
            <div class='card-footer'>
                <a href='marks/setup.php' class='btn btn-primary'>Go to Marks Setup</a>
                <a href='marks/entry.php' class='btn btn-secondary'>Go to Marks Entry</a>
            </div>
        </div>
    </div>
</body>
</html>";
?>