<?php
header('Content-Type: text/plain; charset=utf-8');

require_once __DIR__ . '/env_loader.php';
require_once __DIR__ . '/vendor/autoload.php';

$host = env('REDIS_HOST', 'crm.clientright.ru');
$port = (int)env('REDIS_PORT', 6379);
$password = env('REDIS_PASSWORD', '');

echo "PHP Version: " . PHP_VERSION . "\n";
echo "SAPI: " . php_sapi_name() . "\n";
echo "Подключение к Redis через Predis: $host:$port\n\n";

try {
    $options = [
        'scheme' => 'tcp',
        'host'   => $host,
        'port'   => $port,
        'timeout' => 3.0,
    ];
    if (!empty($password)) {
        $options['password'] = $password;
    }
    
    $redis = new Predis\Client($options);
    $pong = $redis->ping();
    echo "Ping: $pong\n";
    
    // Тест записи/чтения
    $testKey = 'test:predis:web:' . time();
    $redis->setex($testKey, 10, 'hello from web');
    $val = $redis->get($testKey);
    echo "Тест записи/чтения: $val\n";
    $redis->del($testKey);
    
    echo "\n✅ SUCCESS: Redis через Predis работает из веб-сервера!\n";
} catch (Exception $e) {
    echo "❌ ERROR: " . $e->getMessage() . "\n";
}
