<?php
// ============================================================
// connect.php (النسخة الحديثة)
// بوابة التطبيقات (تستلزم JWT)
// ============================================================

require_once __DIR__ . '/core/bootstrap.php';
require_once __DIR__ . '/functions.php';

// 1. Rate Limiting and JWT Authentication
if (!defined('TESTING_BYPASS_AUTH')) {
    $limiter = new RateLimiter($redis);
    $limiter->enforce(RateLimiter::identifier(), 'api');

    $jwtService = new JwtService($redis);
    $decoded = $jwtService->authenticate();

    // متغيرات مساعدة للمطور
    $user_id = $decoded->user_id ?? null;
    $role    = $decoded->role ?? 'passenger';
} else {
    $user_id = $_POST['driver_id'] ?? '2085';
    $role    = 'driver';
}

// 3. Database Connection
try {
    $con = Database::get('main');
} catch (Exception $e) {
    http_response_code(500);
    exit(json_encode(['error' => 'Database connection failed']));
}