<?php

namespace App\Modules\Booking\Application\FindByCode;

use App\Modules\Booking\Application\Find\BookingNotFound;
use App\Modules\Booking\Domain\Booking;
use App\Modules\Booking\Domain\BookingCode;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class FindByCodeBookingQueryHandler implements QueryHandler
{

    public function __construct(
        private BookingByCodeFinder $bookingFinder
    ) {}

    public function __invoke(FindByCodeBookingQuery $query)
    {
        return $this->bookingFinder->__invoke(new BookingCode($query->code))
            ->either(
                fn(BookingNotFound $ex) => throw new NotFoundHttpException($ex->getMessage(), null, $ex->getCode()),
                fn($booking) => new FindByCodeBookingQueryResponse(
                    $booking->Id_Booking,
                    $booking->Booking_Date,
                    $booking->Booking_DateConfirm,
                    $booking->Booking_DateStart,
                    $booking->Booking_DateBriefing,
                    $booking->Booking_PlaceBriefing,
                    $booking->Booking_Code,
                    $booking->Booking_HistoryChanges,
                    $booking->Booking_Hotel,
                    $booking->Booking_Observation,
                    $booking->Booking_InternalRemark,
                    $booking->Booking_Receipt,
                    $booking->Booking_Type,
                    $booking->Booking_LastMinute,
                    $booking->Booking_Assigned,
                    $booking->Booking_Status,
                    $booking->Booking_ManuallyConfirmed,
                    $booking->Booking_InPerson,
                    $booking->Booking_PassengerAmount,
                    $booking->Booking_CheckedInPassengers,
                    $booking->Booking_LastPassenger,
                    $booking->Id_Booking_Replace,
                    $booking->Id_Admin,
                    $booking->Id_GuideLanguage,
                    $booking->Id_Language,
                    $booking->Id_User,
                    $booking->Id_User_Additional,
                    $booking->Id_BookingRelationship,
                    $booking->Id_BookingReferredFrom,
                )
            );
    }
}
