<?php

namespace App\Modules\CommercialInvoice\Application\List;

use App\Modules\Booking\Domain\IdBooking;
use App\Modules\BookingExtraPassenger\Domain\IdBookingExtraPassenger;
use App\Modules\BookingUpgradePassenger\Domain\IdBookingUpgradePassenger;
use App\Modules\CommercialInvoice\Application\CommercialInvoiceResponse;
use App\Modules\CommercialInvoice\Application\CommercialInvoiceResponses;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;

final class ListCommercialInvoiceQueryHandler implements QueryHandler
{
    public function __construct(private CommercialInvoiceLister $lister) {}

    public function __invoke(ListCommercialInvoiceQuery $query): CommercialInvoiceResponses
    {
        return new CommercialInvoiceResponses(...array_map(
            fn($index) => new CommercialInvoiceResponse(
                $index->id()->value(),
                $index->date()->value(),
                $index->dateUpdate()->value(),
                $index->issueDate()->value(),
                $index->codeReservation()->value(),
                $index->numberSerie()->value(),
                $index->type()->value(),
                $index->amountWithoutIgv()->value(),
                $index->amountWithIgv()->value(),
                $index->status()->value(),
                $index->version()->value(),
                $index->idBookingUpgradePassenger()->value(),
                $index->idBookingExtraPassenger()->value(),
                $index->idBooking()->value(),
            ),
            $this->lister->__invoke(
                $query->idBooking() ? new IdBooking($query->idBooking()) : null,
                $query->action(),
                $query->idBookingUpgradePassenger() ? new IdBookingUpgradePassenger($query->idBookingUpgradePassenger()) : null,
                $query->idBookingExtraPassenger() ? new IdBookingExtraPassenger($query->idBookingExtraPassenger()) : null
            )
        ));
    }
}
