<?php

declare(strict_types=1);

namespace App\Config;

use App\Framework\DI\DefaultContainer;
use App\Framework\Http\Middlewares\RateLimitingMiddleware;
use App\Framework\Http\RateLimiter\RateLimiterConfig;
use App\Framework\Http\RateLimiter\RedisRateLimiter;
use Predis\Client as RedisClient;

/**
 * Konfiguriert die Dienste im DI-Container.
 */
return static function (DefaultContainer $container): void {
    // Registrierung von Redis, wenn noch nicht vorhanden
    if (!$container->has(RedisClient::class)) {
        $container->bind(RedisClient::class, function () {
            return new RedisClient([
                'scheme' => $_ENV['REDIS_SCHEME'] ?? 'tcp',
                'host' => $_ENV['REDIS_HOST'] ?? 'redis',
                'port' => (int)($_ENV['REDIS_PORT'] ?? 6379),
            ]);
        });
    }

    // Rate Limiter Konfiguration
    $container->bind(RateLimiterConfig::class, function () {
        $config = new RateLimiterConfig(
            defaultLimit: (int)($_ENV['RATE_LIMIT_DEFAULT'] ?? 60),
            windowSeconds: (int)($_ENV['RATE_LIMIT_WINDOW'] ?? 60)
        );

        // Strengere Limits für sensible Bereiche
        return $config
            ->withAuthLimit((int)($_ENV['RATE_LIMIT_AUTH'] ?? 10), (int)($_ENV['RATE_LIMIT_AUTH_WINDOW'] ?? 300))
            ->withApiLimit((int)($_ENV['RATE_LIMIT_API'] ?? 30), (int)($_ENV['RATE_LIMIT_API_WINDOW'] ?? 60))
            // Weitere Limits hier hinzufügen...
            ;
    });

    // Rate Limiter Service
    $container->bind(RedisRateLimiter::class, function ($container) {
        return new RedisRateLimiter(
            $container->get(RedisClient::class)
        );
    });

    // Rate Limiting Middleware
    $container->bind(RateLimitingMiddleware::class, function ($container) {
        $config = $container->get(RateLimiterConfig::class);
        return new RateLimitingMiddleware(
            $container->get(RedisClient::class),
            $config->getDefaultLimit(),
            $config->getWindowSeconds(),
            $config->getPathLimits()
        );
    });

    // Weitere Dienste hier registrieren...
};
