<?php
/**
 * API PHP pour DTW Boumerdes - Pointage Bus
 * Endpoint: buses.php
 * 
 * Cette API récupère la liste des buses
 */

$db_host = 'localhost';
$db_name = 'transubd_PointageBus';
$db_user = 'transubd_transub_user';
$db_pass = 'Tr@nsub2@24';

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

try {
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $sql = "SELECT * FROM buses ORDER BY code_bus ASC";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    
    $buses = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($buses);
    
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
