<?php
ob_start();
header('Content-Type: application/json');
$response = ['success' => false, 'message' => 'ERROR DESCONOCIDO'];

try {
    require_once('db_connection.php');
    require_once '../files/guardiankey.php';

    $routeId = filter_var($_POST['route_id'] ?? null, FILTER_VALIDATE_INT);
    $routeName = filter_var(trim($_POST['route_name'] ?? ''), FILTER_SANITIZE_FULL_SPECIAL_CHARS);
    $routeDay = filter_var(trim($_POST['route_day'] ?? ''), FILTER_SANITIZE_FULL_SPECIAL_CHARS);

    if (empty($routeId) || empty($routeName)) {
        throw new Exception('DATOS FALTANTES');
    }

    $stmt = $pdo->prepare("UPDATE routes SET route_name = ?, route_day = ? WHERE id = ?");
    $stmt->execute([$routeName, $routeDay, $routeId]);

    $response = ['success' => true, 'message' => 'RUTA ACTUALIZADA'];

} catch (PDOException $e) {
    error_log("Error PDO en routes-update.php: " . $e->getMessage());
    $response = ['success' => false, 'message' => 'ERROR DE BASE DE DATOS'];
} catch (Exception $e) {
    $response = ['success' => false, 'message' => strtoupper($e->getMessage())];
}

if (ob_get_level() > 0) {
    ob_end_clean();
}
echo json_encode($response);
exit;
?>