<?php

namespace App\Http\Controllers\Owner\HtlCampLock;

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\HtlCampLock\Application\Create\CreateHtlCampLockQuery;
use App\Modules\HtlCampLock\Application\Create\CreateHtlCampLockResponse;
use App\Modules\HtlCampLock\Domain\HtlCampLockStatus;
use App\Modules\Shared\Validator\Infraestructure\LaravelValidator;
use App\Modules\HtlCampLock\Infrastructure\HtlCampLockValidators;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Domain\DateStart;
use App\Modules\Shared\Domain\DateTimeStart;
use App\Modules\Shared\Domain\Transaction;

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

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

    public function __invoke(Request $request)
    {
        (new LaravelValidator())->validate($request->all(), $this->domain, HtlCampLockValidators::INSERT);
        $this->transaction->begin();
        /** @var CreateHtlCampLockResponse $response */
        $response = $this->queryBus->ask(new CreateHtlCampLockQuery(
            date(DateTimeStart::$format),
            $request->input('HtlCampLock_DateStart'),
            $request->input('HtlCampLock_Remark'),
            HtlCampLockStatus::ACTIVE,
            $request->input('Id_HtlCamp'),
        ));
        $this->transaction->commit();
        $this->history->insertBasic($request, $response->id());
        return ResponseBuilder::Response([
            "Response_Status"           => 200,
            "Response_Code"             => 200,
            "Response_Data"             => ['id' => $response->id()],
            "Response_Domain"           => $this->domain,
            "Response_Message"          => 'Bloqueo de Campamento agregado con éxito',
            "Response_Error_Message"    => '',
            "Response_Error_Reason"     => '',
        ]);
    }
}
