<?php

namespace App\Http\Controllers\Owner\BriefingPassenger;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\BriefingPassenger\Dto\BriefingPassengerInsertDTO;
use App\Http\Controllers\Owner\c_Admin_Session;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\DefinedService\BriefingPassenger\Application\Insert\InsertBriefingPassengerQuery;
use App\Modules\DefinedService\BriefingPassenger\Application\Insert\InsertBriefingPassengerResponse;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use Illuminate\Http\Request;

final class BriefingPassengerInsertController extends Controller
{
    private string $domain = 'Briefing Passenger';

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

    public function __invoke(Request $request)
    {
        $adminSession = c_Admin_Session::check($request);
        $adminFromSession = (int)($adminSession['Id_Admin'] ?? 0);

        $in = $request->all();
        if (empty($in['idAdmin']) && $adminFromSession > 0) {
            $in['idAdmin'] = $adminFromSession;
        }

        /** @var BriefingPassengerInsertDTO $dto */
        $dto = $this->dtoGenerator->generate(
            $in,
            $this->domain,
            BriefingPassengerInsertDTO::class
        );

        /** @var InsertBriefingPassengerResponse $response */
        $response = $this->queryBus->ask(new InsertBriefingPassengerQuery(
            date('Y-m-d H:i:s'),
            date('Y-m-d H:i:s'),
            $dto->type,
            (string)($dto->hotel ?? ''),
            $dto->time,
            (string)($dto->phone ?? ''),
            (string)($dto->other ?? ''),
            (string)($dto->observations ?? ''),
            $dto->idGroup,
            $dto->idBooking,
            $dto->idBriefingOffice,
            $dto->idPassenger,
            (int)($dto->idAdmin ?? $adminFromSession)
        ));

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