<?php

declare(strict_types=1);

namespace App\Framework\QueryBus;

use App\Framework\DI\Container;
use App\Framework\DI\Initializer;
use App\Framework\Discovery\Results\DiscoveryRegistry;

final readonly class QueryBusInitializer
{
    public function __construct(
        private Container        $container,
        private DiscoveryRegistry $results
    ) {
    }

    #[Initializer]
    public function __invoke(): QueryBus
    {
        $handlerResults = $this->results->attributes->get(QueryHandler::class);

        $handlers = [];
        foreach ($handlerResults as $discoveredAttribute) {
            // Use additionalData instead of mappedData (DiscoveredAttribute)
            if ($discoveredAttribute->additionalData) {
                $handlers[] = $discoveredAttribute->additionalData;
            }
        }

        return new DefaultQueryBus($handlers, $this->container);
    }
}
