<?php

use Apexl\Io\Cache\Factories\RedisCacheDriverFactory;

$redisServerEnvVars = array_filter(
    $_ENV,
    fn(string $key) => str_starts_with($key, 'CACHE_REDIS_SERVER_'),
    ARRAY_FILTER_USE_KEY
);

$redisServers = array_map(fn(string $value, $key) => [
    'server' => $value,
    'port' => (int) \Apexl\Io\env(str_replace('CACHE_REDIS_SERVER_', 'CACHE_REDIS_PORT_', $key), 6379)
], $redisServerEnvVars, array_keys($redisServerEnvVars));

return [
    'enabled' => filter_var(\Apexl\Io\env('CACHE_ENABLED', 'true'), FILTER_VALIDATE_BOOLEAN),
    'register_ephemeral' => filter_var(\Apexl\Io\env('CACHE_REGISTER_EPHEMERAL', 'true'), FILTER_VALIDATE_BOOLEAN),
    'default_stream' => \Apexl\Io\env('CACHE_DEFAULT_STREAM', 'ephemeral'),

    'streams' => [
        [
            'name' => 'redis',
            'factory' => RedisCacheDriverFactory::class,
            'options' => [
                'servers' => $redisServers,
            ],
        ]
    ],
];