<?php
include('includes/db.php');

$class_id = 1; // Change this to an actual class_id from your database
$section_id = 1; // Change this to an actual section_id

$query = "SELECT DISTINCT s.id as subject_id, s.subject_code, s.subject_name, 
                 s.theory_full_marks, s.internal_full_marks, s.type
          FROM class_subject_mapping csm
          JOIN subjects s ON csm.subject_id = s.id
          WHERE csm.class_id = ? 
            AND (csm.section_id = ? OR csm.section_id IS NULL)
            AND s.status = '1'
          ORDER BY s.subject_name";

$stmt = $conn->prepare($query);
$stmt->bind_param("ii", $class_id, $section_id);
$stmt->execute();
$result = $stmt->get_result();

echo "<h3>Debug Subjects Query</h3>";
echo "<p>Class ID: $class_id</p>";
echo "<p>Section ID: $section_id</p>";
echo "<p>Query: $query</p>";

if($result->num_rows > 0) {
    echo "<table border='1'>";
    echo "<tr><th>Subject ID</th><th>Code</th><th>Name</th><th>Theory Marks</th><th>Internal Marks</th><th>Type</th></tr>";
    while($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . $row['subject_id'] . "</td>";
        echo "<td>" . $row['subject_code'] . "</td>";
        echo "<td>" . $row['subject_name'] . "</td>";
        echo "<td>" . $row['theory_full_marks'] . "</td>";
        echo "<td>" . $row['internal_full_marks'] . "</td>";
        echo "<td>" . $row['type'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p>No subjects found. Let's check the data:</p>";
    
    // Check what's in class_subject_mapping
    $check_query = "SELECT * FROM class_subject_mapping WHERE class_id = ? AND (section_id = ? OR section_id IS NULL)";
    $check_stmt = $conn->prepare($check_query);
    $check_stmt->bind_param("ii", $class_id, $section_id);
    $check_stmt->execute();
    $check_result = $check_stmt->get_result();
    
    echo "<h4>class_subject_mapping entries:</h4>";
    if($check_result->num_rows > 0) {
        echo "<table border='1'>";
        echo "<tr><th>ID</th><th>Class ID</th><th>Section ID</th><th>Subject ID</th><th>Subject Name</th></tr>";
        while($row = $check_result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['class_id'] . "</td>";
            echo "<td>" . $row['section_id'] . "</td>";
            echo "<td>" . $row['subject_id'] . "</td>";
            echo "<td>" . $row['subject_name'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "No entries in class_subject_mapping for class_id=$class_id and section_id=$section_id";
    }
    
    // Check subjects table
    echo "<h4>All active subjects:</h4>";
    $subjects_query = "SELECT id, subject_code, subject_name FROM subjects WHERE status = '1'";
    $subjects_result = $conn->query($subjects_query);
    if($subjects_result->num_rows > 0) {
        echo "<table border='1'>";
        echo "<tr><th>ID</th><th>Code</th><th>Name</th></tr>";
        while($row = $subjects_result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['subject_code'] . "</td>";
            echo "<td>" . $row['subject_name'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "No active subjects found";
    }
}

$stmt->close();
?>