<?php

namespace App\Modules\WebContactMessageDetail\Application\Update;

use App\Modules\Package\Domain\IdPackage;
use App\Modules\WebContactMessageDetail\Domain\IdWebContactMessageDetail;
use App\Modules\WebContactMessageDetail\Domain\WebContactMessageDetail;
use App\Modules\WebContactMessageDetail\Domain\WebContactMessageDetailStatus;
use App\Modules\Shared\Domain\Bus\Query\QueryHandler;
use App\Modules\WebContactMessage\Domain\IdWebContactMessage;
use App\Modules\WebContactMessageDetail\Application\Index\WebContactMessageDetailIndexer;
use App\Modules\WebContactMessageDetail\Domain\WebContactMessageDetailDateStart;
use App\Modules\WebContactMessageDetail\Domain\WebContactMessageDetailPackage;

final class UpdateWebContactMessageDetailQueryHandler implements QueryHandler
{
    public function __construct(
        private WebContactMessageDetailUpdater $updater,
        private WebContactMessageDetailIndexer $indexer
    ) {}

    public function __invoke(UpdateWebContactMessageDetailQuery $query): UpdateWebContactMessageDetailQueryResponse
    {
        $response = $this->indexer->__invoke(new IdWebContactMessageDetail($query->id()));
        $index = $response->response()[0];
        $model = new WebContactMessageDetail(
            new WebContactMessageDetailDateStart($query->dateStart()),
            new WebContactMessageDetailPackage($index->WebContactMessageDetail_Package),
            new WebContactMessageDetailStatus($index->WebContactMessageDetail_Status),
            new IdWebContactMessage($index->Id_WebContactMessage),
            new IdPackage($index->Id_Package),
            new IdWebContactMessageDetail($query->id()),
        );
        return $this->updater->__invoke($model);
    }
}
