<?php

namespace App\Http\Controllers\Owner\Location;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\c_Admin_Session;
use App\Http\Controllers\Owner\Location\Dto\LocationInsertDTO;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\DefinedService\Location\Application\Insert\InsertLocationQuery;
use App\Modules\DefinedService\Location\Application\Insert\InsertLocationResponse;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use Illuminate\Http\Request;

class LocationInsertController extends Controller
{

    private string $domain = 'Location';

    public function __construct(
        private QueryBus $queryBus,
        private LaravelDTOGenerator $dtoGenerator
    ) {}

    public function __invoke(Request $request)
    {
        /** @var LocationInsertDTO $dto */
        $dto = $this->dtoGenerator->generate($request->all(), $this->domain, LocationInsertDTO::class);
        $adminSession = c_Admin_Session::check($request);
        // $idAdmin = 
        /** @var InsertLocationResponse $response */
        $response = $this->queryBus->ask(new InsertLocationQuery(
            date('Y-m-d H:i:s'),
            date('Y-m-d H:i:s'),
            $dto->Location_Name,
            $dto->Location_Abrv ?? '',
            $dto->Location_Latitude ?? '',
            $dto->Location_Longitude ?? '',
            $dto->Location_Status ?? 1,
            $adminSession['Id_Admin']
        ));

        return ResponseBuilder::Response([
            "Response_Status"           => 200,
            "Response_Code"             => 200,
            "Response_Data"             => ['id' => $response->id()],
            "Response_Domain"           => $this->domain,
            "Response_Message"          => 'Ubicación agregada con éxito',
            "Response_Error_Message"    => '',
            "Response_Error_Reason"     => ''
        ]);
    }
}
