<?php

namespace App\Http\Controllers\Owner\WebContactMessage;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\c_Admin_Session;
use App\Http\Controllers\Owner\WebContactMessage\Dto\WebContactMessageUpdateDTO;
use Illuminate\Http\Request;
use App\Http\Helpers\ResponseBuilder;
use App\Http\History\HistoryData;
use App\Http\History\HistoryTables;
use App\Modules\History\Application\Insert\InsertHistoryQuery;
use App\Modules\History\Domain\HistoryOperation;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use App\Modules\WebContactMessage\Application\Index\IndexWebContactMessageQuery;
use App\Modules\WebContactMessage\Application\Index\IndexWebContactMessageQueryResponse;
use App\Modules\WebContactMessage\Application\Update\UpdateWebContactMessageQuery;
use App\Modules\WebContactMessage\Application\Update\UpdateWebContactMessageQueryResponse;

class WebContactMessageUpdateController extends Controller
{
    private $domain;
    /** @var HistoryData */
    private $history;

    public function __construct(
        private QueryBus $queryBus,
        private LaravelDTOGenerator $dtoGenerator,
    ) {
        $this->domain = "WebContactMessage";
        $this->history = new HistoryData(HistoryTables::WEB_CONTACT_MESSAGE);
    }

    public function __invoke(Request $request)
    {
        /** @var WebContactMessageUpdateDTO $dto */
        $dto = $this->dtoGenerator->generate($request->all(), $this->domain, WebContactMessageUpdateDTO::class);
        $adminSession = c_Admin_Session::check($request);

        /** @var IndexWebContactMessageQueryResponse $firstResponse */
        $firstResponse = $this->queryBus->ask(new IndexWebContactMessageQuery($dto->Id_WebContactMessage));

        /** @var UpdateWebContactMessageQueryResponse $response */
        $response = $this->queryBus->ask(new UpdateWebContactMessageQuery(
            $dto->Id_WebContactMessage,
            $dto->WebContactMessage_DateReception,
            $dto->WebContactMessage_Message,
            $dto->WebContactMessage_Adults,
            $dto->WebContactMessage_Children,
            $dto->WebContactMessage_Subscription,
            $dto->WebContactMessage_Status,
            // ---------------------------------------------------------------------------
            $dto->WebContactMessage_DateFirstContact,
            $dto->WebContactMessage_DateAssign,
            $dto->WebContactMessage_DateFirstAnswer,
            $dto->WebContactMessage_FirstMessage,
            $dto->WebContactMessage_Account,
            $dto->WebContactMessage_LostMessage,
            $dto->WebContactMessage_LostDate,
            $dto->WebContactMessage_Name,
            $dto->WebContactMessage_LastName,
            $dto->WebContactMessage_Email,
            $dto->WebContactMessage_PhoneCode,
            $dto->WebContactMessage_Phone,
            $dto->WebContactMessage_AdminRemark,
            $dto->WebContactMessage_Route,
            $dto->Id_Country,
            $dto->Id_Country_Current,
            $dto->Id_Language,
            $dto->Id_Channel,
            $dto->Id_Language_Contact,
            $dto->Id_Language_Answer,
            $dto->Id_WebTypeClient,
            $adminSession['Id_Admin'],
            $dto->Id_Admin,
            $dto->Id_WebPage
        ));

        /** @var IndexWebContactMessageQueryResponse $lastResponse */
        $lastResponse = $this->queryBus->ask(new IndexWebContactMessageQuery($dto->Id_WebContactMessage));

        $this->queryBus->ask(new InsertHistoryQuery(
            json_encode($firstResponse->response()),
            json_encode($lastResponse->response()),
            HistoryOperation::UPDATE,
            $dto->Id_WebContactMessage,
            $dto->Id_WebContactMessage,
            HistoryTables::WEB_CONTACT_MESSAGE,
            $adminSession['Id_Admin'],
        ));

        // $this->history->updateBasic($dto->Id_WebContactMessage, $request);
        return ResponseBuilder::Response($response->response());
    }
}
