<?php

namespace App\Modules\PaxInformationRoom\Application\List;

use App\Modules\PaxInformation\Domain\IdPaxInformation;
use App\Modules\PaxInformationRoom\Application\PaxInformationRoomResponse;
use App\Modules\PaxInformationRoom\Application\PaxInformationRoomResponses;
use App\Modules\PaxInformationRoom\Domain\PaxInformationRoomDescription;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;

final class ListPaxInformationRoomQueryHandler implements QueryHandler
{
    public function __construct(private PaxInformationRoomLister $lister) {}

    public function __invoke(ListPaxInformationRoomQuery $query): PaxInformationRoomResponses
    {
        return new PaxInformationRoomResponses(...array_map(
            (fn($pir) => $pir->toDTO(
                PaxInformationRoomResponse::class,
                add: [
                    'PaxInformationRoomPassengers' => array_map(fn($pirp) => $pirp->toDTO(), $pir->paxInformationRoomPassengers())
                ]
            )),
            $this->lister->__invoke(
                new IdPaxInformation($query->idPaxInformation),
                $query->action,
                new ListPaxInformationRoomPopulate($query->populate)
            )
        ));
    }
}
