<?php
// Test script to verify passing marks configuration

// Simulate database connection and query
echo "<h2>Testing Passing Marks Configuration</h2>\n";

// Simulate subject_mark_distribution data
$subject_mark_distribution_data = [
    [
        'subject_id' => 1,
        'class_id' => 11,
        'session' => '2024',
        'exam_type' => 'midterm',
        'total_marks' => 100,
        'overall_pass_marks' => 33
    ],
    [
        'subject_id' => 2,
        'class_id' => 11,
        'session' => '2024',
        'exam_type' => 'midterm',
        'total_marks' => 100,
        'overall_pass_marks' => 33
    ]
];

// Simulate combined Bangla subject
$combined_bangla = [
    'subject_id' => 'combined_bangla',
    'subject_name' => 'Combined Bangla',
    'full_marks' => 0,
    'pass_marks' => 0
];

// Simulate Bangla results
$bangla_results = [
    [
        'subject_id' => 1,
        'subject_name' => 'বাংলা'
    ]
];

// Simulate student data
$student = [
    'class_id' => 11
];

$session = '2024';
$exam_type = 'midterm';

echo "<h3>Testing Combined Bangla Subject</h3>\n";
echo "Before processing:<br>\n";
echo "Full Marks: " . $combined_bangla['full_marks'] . "<br>\n";
echo "Pass Marks: " . $combined_bangla['pass_marks'] . "<br>\n";

// Simulate the logic we implemented
if ($combined_bangla['subject_id'] == 'combined_bangla' || $combined_bangla['subject_id'] == 'combined_english') {
    // For combined subjects, try to get actual marks from subject_mark_distribution
    // Use a generic subject ID to fetch the passing marks (we'll use the first Bangla/English subject ID)
    $combined_subject_id = null;
    if ($combined_bangla['subject_id'] == 'combined_bangla' && !empty($bangla_results)) {
        $combined_subject_id = $bangla_results[0]['subject_id'];
    }
    
    if ($combined_subject_id) {
        // Simulate database query
        $found_record = null;
        foreach ($subject_mark_distribution_data as $record) {
            if ($record['subject_id'] == $combined_subject_id && 
                $record['class_id'] == $student['class_id'] && 
                $record['session'] == $session && 
                $record['exam_type'] == $exam_type) {
                $found_record = $record;
                break;
            }
        }
        
        if ($found_record) {
            $combined_bangla['full_marks'] = $found_record['total_marks'];
            $combined_bangla['pass_marks'] = $found_record['overall_pass_marks'];
        } else {
            // Fallback to defaults
            if (!isset($combined_bangla['full_marks']) || $combined_bangla['full_marks'] == 0) {
                $combined_bangla['full_marks'] = 100;
            }
            if (!isset($combined_bangla['pass_marks']) || $combined_bangla['pass_marks'] == 0) {
                $combined_bangla['pass_marks'] = 33;
            }
        }
    } else {
        // Fallback to defaults
        if (!isset($combined_bangla['full_marks']) || $combined_bangla['full_marks'] == 0) {
            $combined_bangla['full_marks'] = 100;
        }
        if (!isset($combined_bangla['pass_marks']) || $combined_bangla['pass_marks'] == 0) {
            $combined_bangla['pass_marks'] = 33;
        }
    }
}

echo "After processing:<br>\n";
echo "Full Marks: " . $combined_bangla['full_marks'] . "<br>\n";
echo "Pass Marks: " . $combined_bangla['pass_marks'] . "<br>\n";
echo "Expected: Full Marks: 100, Pass Marks: 33<br>\n";

echo "<h3>Verification:</h3>\n";
echo "Combined subjects should now use the passing marks configured in the setup system<br>\n";
echo "Regular subjects continue to work as before<br>\n";
?>