<?php

namespace App\Modules\EndpointScope\Application\List;

use App\Modules\EndpointScope\Application\EndpointScopeItemResponse;
use App\Modules\EndpointScope\Application\EndpointScopeItemResponses;
use App\Modules\EndpointScope\Domain\EndpointScope;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;

final class ListEndpointScopeQueryHandler implements QueryHandler
{
    public function __construct(
        private EndpointScopeLister $lister
    ) {}

    public function __invoke(ListEndpointScopeQuery $query): EndpointScopeItemResponses
    {
        $scopes = $this->lister->__invoke(new ListEndpointScopeQuery($query->endpointPath, $query->endpointMethod));

        return new EndpointScopeItemResponses(
            ...array_map(
                fn(EndpointScope $scope) => new EndpointScopeItemResponse(
                    Id_EndpointScope: $scope->id()->value(),
                    EndpointScope_Status: $scope->status()->value(),
                    Id_Scope: $scope->idScope()->value(),
                    Id_Endpoint: $scope->idEndpoint()->value(),
                ),
                $scopes
            )
        );
    }
}
