<?php

namespace App\Framework\Core;

use App\Framework\Cache\Cache;
use App\Framework\Cache\CacheInitializer;
use App\Framework\Config\Configuration;
use App\Framework\DI\Container;
use App\Framework\DI\Initializer;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Discovery\Results\DiscoveryResults;
use App\Framework\Http\ResponseEmitter;
use App\Framework\Performance\PerformanceMeter;
use App\Framework\Attributes\Route;

final readonly class ContainerBootstrapper
{
    public function __construct(
        private Container $container)
    {}
    public function bootstrap(
        string $basePath,
        PerformanceMeter $meter,
        array $config = []): void
    {
        // Kern-Dienste im Container registrieren:
        $this->container->instance(PerformanceMeter::class, $meter);
        $this->container->instance(Cache::class, new CacheInitializer()());
        $this->container->instance(Configuration::class, new Configuration($config));
        $this->container->instance(PathProvider::class, new PathProvider($basePath));
        $this->container->instance(ResponseEmitter::class, new ResponseEmitter());

        $this->autowire($this->container->get(PathProvider::class), $this->container->get(Configuration::class));
    }

    private function autowire(PathProvider $pathProvider, Configuration $configuration):void
    {
        // Im ContainerBootstrapper
        $bootstrapper = new DiscoveryServiceBootstrapper($this->container);
        $results = $bootstrapper->bootstrap();

        // Ergebnisse sind automatisch im Container verfügbar
        /** @var DiscoveryResults $results */
        $results = $this->container->get(DiscoveryResults::class);

        #$routeResults = $results->get(Route::class);


        // Fallback: Versuche auch mit führendem Backslash
        /*if (empty($routeResults)) {
            $routeResults = $results->get('\\' . $routeClass);
            debug('Tried with leading backslash');
        }*/

        // Fallback: Suche in allen verfügbaren Keys
        /*if (empty($routeResults)) {
            foreach (array_keys($results->getAllAttributeResults()) as $key) {
                if (str_contains($key, 'Route')) {
                    debug("Found Route-related key: $key");
                    $routeResults = $results->get($key);
                    break;
                }
            }
        }*/

        #$discovery = new AttributeDiscoveryService($this->container, $pathProvider, $configuration);

        #$dresults = ProcessedResults::fromArray($discovery->discoverAndProcess())->get(Route::class);

        #debug(count($routeResults));
        #dd(count($dresults));




        #$cache = $this->container->get(Cache::class);

        #$cache->forget('discovery_service');

        #$processedResults = $cache->remember('discovery_service', function () use ($discovery) {
#
       #     // Attribute entdecken und verarbeiten
        #    return $discovery->discoverAndProcess();
#
       # })->value;

        #$results = ProcessedResults::fromArray($processedResults);

        #$this->container->instance(ProcessedResults::class, $results);

        #$this->executeInitializers($results);

    }

    private function executeInitializers(DiscoveryResults $initializerClasses): void
    {
        foreach ($initializerClasses->get(Initializer::class) as $initializerClass) {

            $instance = $this->container->invoker->invoke($initializerClass['class'], $initializerClass['method']);

            $this->container->instance($initializerClass['return'], $instance);
        }
    }
}
