<?php
// Script to fix the issue where combined subjects' full_marks and pass_marks are being overridden
$file_path = 'reports/bulk_print_marksheets.php';
$content = file_get_contents($file_path);

// Fix: Modify the section that overrides full_marks and pass_marks for combined subjects
// We need to preserve the values set by the combine functions for combined subjects

// Pattern to match the section that overrides the values
$pattern = '/(foreach \(\$results as &\$result\) \{\s*if \(\$result\[\'subject_id\'\] == \'combined_bangla\' \|\| \$result\[\'subject_id\'\] == \'combined_english\'\) \{[\s\S]*?\/\/ Fallback to defaults\s*if \(\!isset\(\$result\[\'full_marks\'\]\) \|\| \$result\[\'full_marks\'\] == 0\) \{[\s\S]*?\$result\[\'pass_marks\'\] = 40;\s*\}[\s\S]*?\}[\s\S]*?\})/s';

// Replacement that preserves combined subjects' values
$replacement = 'foreach ($results as &$result) {
            // Preserve values for combined subjects set by combine functions
            $is_combined_subject = ($result[\'subject_id\'] == \'combined_bangla\' || $result[\'subject_id\'] == \'combined_english\');
            $original_full_marks = $is_combined_subject && isset($result[\'full_marks\']) ? $result[\'full_marks\'] : null;
            $original_pass_marks = $is_combined_subject && isset($result[\'pass_marks\']) ? $result[\'pass_marks\'] : null;
            
            if ($is_combined_subject) {
                // 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 ($result[\'subject_id\'] == \'combined_bangla\' && !empty($original_bangla_results)) {
                    $combined_subject_id = $original_bangla_results[0][\'subject_id\'];
                } else if ($result[\'subject_id\'] == \'combined_english\' && !empty($original_english_results)) {
                    $combined_subject_id = $original_english_results[0][\'subject_id\'];
                }
                
                if ($combined_subject_id) {
                    $stmt = $pdo->prepare("SELECT total_marks, overall_pass_marks FROM subject_mark_distribution WHERE subject_id = ? AND class_id = ? AND session = ? AND exam_type = ? LIMIT 1");
                    $stmt->execute([$combined_subject_id, $student[\'class_id\'], $session, $exam_type]);
                    $smd = $stmt->fetch();
                    
                    if ($smd) {
                        // Only override if we didn\'t get proper values from combine functions
                        if ($original_full_marks === null) {
                            $result[\'full_marks\'] = $smd[\'total_marks\'];
                        } else {
                            $result[\'full_marks\'] = $original_full_marks;
                        }
                        
                        if ($original_pass_marks === null) {
                            $result[\'pass_marks\'] = $smd[\'overall_pass_marks\'];
                        } else {
                            $result[\'pass_marks\'] = $original_pass_marks;
                        }
                    } else {
                        // Fallback to defaults only if we didn\'t get proper values from combine functions
                        if ($original_full_marks === null || $original_full_marks == 0) {
                            $result[\'full_marks\'] = 100;
                        } else {
                            $result[\'full_marks\'] = $original_full_marks;
                        }
                        
                        if ($original_pass_marks === null || $original_pass_marks == 0) {
                            $result[\'pass_marks\'] = 40;
                        } else {
                            $result[\'pass_marks\'] = $original_pass_marks;
                        }
                    }
                } else {
                    // Fallback to defaults only if we didn\'t get proper values from combine functions
                    if ($original_full_marks === null || $original_full_marks == 0) {
                        $result[\'full_marks\'] = 100;
                    } else {
                        $result[\'full_marks\'] = $original_full_marks;
                    }
                    
                    if ($original_pass_marks === null || $original_pass_marks == 0) {
                        $result[\'pass_marks\'] = 40;
                    } else {
                        $result[\'pass_marks\'] = $original_pass_marks;
                    }
                }
            }
        }';

$content = preg_replace($pattern, $replacement, $content);

// Write the fixed content back to the file
file_put_contents($file_path, $content);

echo "Fix v3 applied successfully!\n";
?>