<?php

namespace App\Http\Controllers\Owner\GroupPassengerMedia;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Helpers\ResponseBuilder;
use App\Http\History\HistoryData;
use App\Http\History\HistoryTables;
use App\Modules\Shared\Validator\Infraestructure\LaravelValidator;
use App\Modules\GroupPassengerMedia\Application\Update\UpdateGroupPassengerMediaQuery;
use App\Modules\GroupPassengerMedia\Infrastructure\GroupPassengerMediaValidators;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Domain\Transaction;

class GroupPassengerMediaUpdateController extends Controller
{
    private $domain;
    private HistoryData $history;

    public function __construct(
        private QueryBus $queryBus,
        private Transaction $transaction
    ) {
        $this->domain = "GroupPassengerMedia";
        $this->history = new HistoryData(HistoryTables::GROUP_PROVIDER_MEDIA);
    }

    public function __invoke(Request $request)
    {
        (new LaravelValidator())->validate($request->all(), $this->domain, GroupPassengerMediaValidators::UPDATE);
        $this->history->obtainOld($request->input('Id_GroupPassengerMedia'));
        $this->transaction->begin();
        $this->queryBus->ask(new UpdateGroupPassengerMediaQuery(
            $request->input('Id_GroupPassengerMedia'),
            $request->input('GroupPassengerMedia_Name'),
            $request->input('GroupPassengerMedia_Abrv'),
            $request->input('GroupPassengerMedia_Status'),
        ));
        $this->transaction->commit();
        $this->history->updateBasic($request->input('Id_GroupPassengerMedia'), $request);
        return ResponseBuilder::Response([
            "Response_Status"           => 200,
            "Response_Code"             => 200,
            "Response_Data"             => [],
            "Response_Domain"           => $this->domain,
            "Response_Message"          => 'Moneda de Facturación modificada con éxito',
            "Response_Error_Message"    => '',
            "Response_Error_Reason"     => '',
        ]);
    }
}
