<?php

namespace App\Modules\WebContactMessage\Application\UpdateIdBooking;

use App\Modules\Booking\Domain\IdBooking;
use App\Modules\WebContactMessage\Domain\IdWebContactMessage;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;
use App\Modules\WebContactMessage\Domain\ExistsWebContactMessage;
use App\Modules\WebContactMessage\Domain\WebContactMessage;
use App\Modules\WebContactMessage\Domain\WebContactMessageRepository;

final class UpdateIdBookingWebContactMessageQueryHandler implements QueryHandler
{
    public function __construct(
        private WebContactMessageIdBookingUpdater $updater,
        private WebContactMessageRepository $repository,
    ) {}

    public function __invoke(UpdateIdBookingWebContactMessageQuery $query): UpdateIdBookingWebContactMessageQueryResponse
    {
        /** @var WebContactMessage $webContactMessage */
        $webContactMessage = $this->repository->search(new ExistsWebContactMessage(new IdWebContactMessage($query->id)));
        $webContactMessage->setIdBooking(new IdBooking($query->idBooking));
        $this->updater->__invoke($webContactMessage);
        $this->repository->reloadStatusByBooking(new IdWebContactMessage($query->id));
        return new UpdateIdBookingWebContactMessageQueryResponse();
    }
}
