<?php
// api/results/get_sections.php
require_once '../../includes/config.php'; // Ensure this contains your PDO $pdo connection

header('Content-Type: application/json');

try {
    // 1. Get and validate input
    $class_id = isset($_GET['class_id']) ? intval($_GET['class_id']) : 0;

    if ($class_id <= 0) {
        echo json_encode(["status" => "success", "data" => []]);
        exit;
    }

    // 2. Secure PDO Prepared Statement
    $query = "SELECT id, section_name FROM sections 
              WHERE class_id = :class_id 
              AND status = 'active' 
              ORDER BY section_name ASC";
              
    $stmt = $pdo->prepare($query);
    $stmt->execute([':class_id' => $class_id]);
    
    // 3. Fetch all results as an associative array
    $sections = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 4. Return standardized JSON
    echo json_encode([
        "status" => "success",
        "data" => $sections
    ]);

} catch (PDOException $e) {
    // Log error internally, show generic message to user
    error_log("Database Error: " . $e->getMessage());
    http_response_code(500);
    echo json_encode([
        "status" => "error",
        "message" => "Internal Server Error"
    ]);
}?>