<?php
session_start();
require_once 'config/database.php';

// Get class 10
$stmt = $pdo->prepare("SELECT * FROM classes WHERE name = 'Ten' AND status = 'active'");
$stmt->execute();
$class_10 = $stmt->fetch();

if (!$class_10) {
    die("Class 10 not found");
}

echo "<h2>Corrected Debug for Class 10 Subjects Issue</h2>";
echo "<p>Class: " . $class_10['name'] . " (ID: " . $class_10['id'] . ")</p>";

// Get a specific student with 14 subjects (Roll 1)
$stmt = $pdo->prepare("
    SELECT s.id, s.student_id, s.first_name, s.last_name, s.roll_number
    FROM students s
    WHERE s.class_id = ? AND s.status = 'active' AND s.roll_number = 1
");
$stmt->execute([$class_10['id']]);
$student = $stmt->fetch();

if (!$student) {
    die("Student not found");
}

echo "<h3>Student: " . $student['first_name'] . " " . $student['last_name'] . " (Roll: " . $student['roll_number'] . ")</h3>";

// Get all subjects selected by the student
$stmt = $pdo->prepare("
    SELECT ss.subject_id, ss.is_fourth_subject, s.name as subject_name, s.code as subject_code 
    FROM student_subjects ss 
    JOIN subjects s ON ss.subject_id = s.id 
    WHERE ss.student_id = ? AND s.status = 'active' 
    ORDER BY s.name
");
$stmt->execute([$student['id']]);
$all_student_subjects = $stmt->fetchAll();

echo "<h4>All Student Subjects (" . count($all_student_subjects) . " found)</h4>";
echo "<ul>";
foreach ($all_student_subjects as $subject) {
    $is_fourth = ($subject['is_fourth_subject'] ?? false) ? " (4th)" : "";
    echo "<li>" . $subject['subject_name'] . " (" . $subject['subject_code'] . ") (ID: " . $subject['subject_id'] . ")" . $is_fourth . "</li>";
}
echo "</ul>";

// Separate into different categories correctly
$bangla_subjects = [];
$english_subjects = [];
$other_subjects = [];

foreach ($all_student_subjects as $subject) {
    // Check if this is a Bangla subject (Bangla or Bangla Grammar)
    if ((stripos($subject['subject_name'], 'বাংলা') !== false && stripos($subject['subject_name'], 'বাংলাদেশ') === false) || 
        (stripos($subject['subject_name'], 'Bangla') !== false && stripos($subject['subject_name'], 'Bangladesh') === false)) {
        $bangla_subjects[] = $subject;
    }
    // Check if this is an English subject (English or English Grammar)
    else if (stripos($subject['subject_name'], 'ইংরেজি') !== false || stripos($subject['subject_name'], 'English') !== false) {
        $english_subjects[] = $subject;
    } else {
        $other_subjects[] = $subject;
    }
}

echo "<h4>Subject Categories:</h4>";
echo "<p>Bangla subjects: " . count($bangla_subjects) . "</p>";
echo "<ul>";
foreach ($bangla_subjects as $subject) {
    echo "<li>" . $subject['subject_name'] . "</li>";
}
echo "</ul>";

echo "<p>English subjects: " . count($english_subjects) . "</p>";
echo "<ul>";
foreach ($english_subjects as $subject) {
    echo "<li>" . $subject['subject_name'] . "</li>";
}
echo "</ul>";

echo "<p>Other subjects: " . count($other_subjects) . "</p>";
echo "<ul>";
foreach ($other_subjects as $subject) {
    $is_fourth = ($subject['is_fourth_subject'] ?? false) ? " (4th)" : "";
    echo "<li>" . $subject['subject_name'] . $is_fourth . "</li>";
}
echo "</ul>";

// Check for fourth subjects among all subjects
$fourth_subjects = array_filter($all_student_subjects, function($subject) {
    $isFourth = $subject['is_fourth_subject'] ?? false;
    return ($isFourth === true || $isFourth === 1 || $isFourth === '1' || $isFourth === 'true');
});

echo "<h4>Fourth Subjects (" . count($fourth_subjects) . " found)</h4>";
if (count($fourth_subjects) > 0) {
    echo "<ul>";
    foreach ($fourth_subjects as $subject) {
        echo "<li>" . $subject['subject_name'] . " (" . $subject['subject_code'] . ")</li>";
    }
    echo "</ul>";
} else {
    echo "<p>No fourth subjects found</p>";
}

// Simulate the correct combination logic
$combined_subject_ids = [];

// Add Bangla subject IDs to combined list
foreach ($bangla_subjects as $bangla_subject) {
    $combined_subject_ids[] = $bangla_subject['subject_id'];
}

// Add English subject IDs to combined list
foreach ($english_subjects as $english_subject) {
    $combined_subject_ids[] = $english_subject['subject_id'];
}

echo "<h4>Combined Subject IDs:</h4>";
echo "<p>" . implode(', ', $combined_subject_ids) . "</p>";

// Count final subjects (excluding combined ones)
$final_subject_count = 0;
$displayed_subjects = [];

// Add combined Bangla if exists (counts as 1 subject)
if (!empty($bangla_subjects)) {
    $final_subject_count++;
    $displayed_subjects[] = "বাংলা(একত্রে)";
    echo "<p>Adding combined Bangla subject</p>";
}

// Add combined English if exists (counts as 1 subject)
if (!empty($english_subjects)) {
    $final_subject_count++;
    $displayed_subjects[] = "ইংরেজি(একত্রে)";
    echo "<p>Adding combined English subject</p>";
}

// Add other subjects (excluding combined ones)
$non_combined_count = 0;
foreach ($other_subjects as $subject) {
    if (!in_array($subject['subject_id'], $combined_subject_ids)) {
        $final_subject_count++;
        $non_combined_count++;
        $displayed_subjects[] = $subject['subject_name'];
    }
}

echo "<p>Adding " . $non_combined_count . " non-combined subjects</p>";

echo "<h4>Final Subject Count: " . $final_subject_count . "</h4>";
echo "<h4>Displayed Subjects (" . count($displayed_subjects) . "):</h4>";
echo "<ul>";
foreach ($displayed_subjects as $subject_name) {
    echo "<li>" . $subject_name . "</li>";
}
echo "</ul>";

// Check if there's any issue with the fourth subject
$fourth_subject_in_combined = false;
foreach ($fourth_subjects as $fourth_subject) {
    if (in_array($fourth_subject['subject_id'], $combined_subject_ids)) {
        $fourth_subject_in_combined = true;
        echo "<p>Fourth subject '" . $fourth_subject['subject_name'] . "' is in combined subjects</p>";
    }
}

if ($fourth_subject_in_combined) {
    echo "<p><strong>Note:</strong> If a fourth subject is among the combined Bangla/English subjects, it should still be displayed separately according to the Jessore Board rules.</p>";
}

?>