<?php

declare(strict_types=1);

namespace App\Framework\DDoS;

use App\Framework\Cache\Cache;
use App\Framework\DateTime\Clock;
use App\Framework\DDoS\Analysis\GeoAnomalyDetector;
use App\Framework\DDoS\Analysis\TrafficPatternAnalyzer;
use App\Framework\DI\Initializer;
use App\Framework\ErrorHandling\SecurityEventLogger;
use App\Framework\Logging\Logger;
use App\Framework\Performance\PerformanceService;

/**
 * DDoS Protection Engine Initializer
 *
 * Temporary initializer to provide DDoSProtectionEngine dependency for middleware
 */
final readonly class DDoSProtectionEngineInitializer
{
    public function __construct(
        private Logger $logger,
        private Clock $clock,
        private PerformanceService $performance,
        private SecurityEventLogger $securityLogger,
        private Cache $cache
    ) {
    }

    #[Initializer]
    public function __invoke(): DDoSProtectionEngine
    {
        // Create minimal mock dependencies for development
        $config = new DDoSConfig();

        // Create real TrafficPatternAnalyzer (it's a concrete class, not interface)
        $trafficAnalyzer = new TrafficPatternAnalyzer(
            $this->cache,
            $this->clock,
            $this->logger
        );

        // Create real GeoAnomalyDetector (it's a concrete class, not interface)
        $geoDetector = new GeoAnomalyDetector(
            $this->cache,
            $this->clock,
            $this->logger
        );

        // Create minimal working ResponseSystem - using existing dependency
        // This is temporary until proper DDoS interfaces are implemented
        $responseSystem = new class () {
            public function generateResponse($assessment, $request)
            {
                return new \App\Framework\DDoS\Response\ValueObjects\DDoSResponse(
                    \App\Framework\DDoS\Response\ValueObjects\ResponseType::ALLOW,
                    200,
                    [],
                    ''
                );
            }
        };

        // Mock the other required components (remove interface implementations for now)
        $threatCalculator = new class () {
            public function calculateThreatLevel($data)
            {
                return 0.0;
            }
        };

        $patternDetector = new class () {
            public function detectPatterns($request)
            {
                return [];
            }
        };

        $requestAnalyzer = new class () {
            public function analyzeRequest($request)
            {
                return [];
            }
        };

        $serviceAnalyzer = new class () {
            public function analyzeHealth()
            {
                return true;
            }
        };

        $wafAnalyzer = new class () {
            public function adaptAnalysis($wafData)
            {
                return [];
            }
        };

        return new DDoSProtectionEngine(
            $trafficAnalyzer,
            $geoDetector,
            $responseSystem,
            $this->securityLogger,
            $this->performance,
            $this->logger,
            $this->clock,
            $config,
            $threatCalculator,
            $patternDetector,
            $requestAnalyzer,
            $serviceAnalyzer,
            $wafAnalyzer
        );
    }
}
