<?php
// Prevent any accidental output (whitespace) before the header
ob_start();
header('Content-Type: application/json');

require_once '../../includes/config.php';

/**
 * We use a JOIN to get the human-readable Class Name 
 * while keeping the query safe with PDO.
 */
try {
    $sql = "SELECT 
    sections.id, 
    sections.section_name, 
    sections.status, 
    sections.class_id,   -- <--- YOU ARE MISSING THIS LINE
    classes.class_name 
FROM sections 
INNER JOIN classes ON sections.class_id = classes.id";

    $stmt = $pdo->query($sql);
    
    // Fetch as Associative Array
    $sections = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Return a structured response
    echo json_encode($sections);

} catch (PDOException $e) {
    // In production, log $e->getMessage() to a file and show a clean error to user
    http_response_code(500);
    echo json_encode([
        'status' => 'error', 
        'message' => 'Database error occurred. Please contact the administrator.'
    ]);
}

// Clear the buffer
ob_end_flush();
?>