<?php

namespace App\Http\Controllers\Owner\Room;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\Room\Dto\RoomListNoRoomDTO;
use App\Http\Helpers\ResponseBuilder;
use App\Modules\Room\Application\ListAllNoRoom\ListAllNoRoomQuery;
use App\Modules\Room\Application\ListAllNoRoom\ListAllNoRoomResponse;
use App\Modules\Room\Application\ListAllNoRoomCount\ListAllCountNoRoomQuery;
use App\Modules\Room\Application\ListAllNoRoomCount\ListAllCountNoRoomResponse;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use Illuminate\Http\Request;

class RoomListNoRoomController extends Controller
{
    private $domain = 'Room';

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

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

        $allExtras          = ($dto->AllExtras ?? 1) == 2;
        $allTours           = ($dto->AllTours ?? 1) == 2;
        $allUpgradeStatuses = ($dto->AllUpgradeStatuses ?? 1) == 2;

        /** @var ListAllNoRoomResponse $response */
        $response = $this->queryBus->ask(new ListAllNoRoomQuery(
            $dto->DateStart,
            $dto->DateEnd,
            $dto->OrderBy,
            $dto->OrderType,
            $dto->IdExtras,
            $dto->IdTours,
            $dto->UpgradeStatuses,
            $allExtras,
            $allTours,
            $allUpgradeStatuses,
            $dto->Location ?? 0,
            $dto->Search,
            $dto->Page,
            $dto->PageSize,
        ));

        /** @var ListAllCountNoRoomResponse $count */
        $count = $this->queryBus->ask(new ListAllCountNoRoomQuery(
            $dto->DateStart,
            $dto->DateEnd,
            $dto->OrderBy,
            $dto->OrderType,
            $dto->IdExtras,
            $dto->IdTours,
            $dto->UpgradeStatuses,
            $allExtras,
            $allTours,
            $allUpgradeStatuses,
            $dto->Location ?? 0,
            $dto->Search,
        ));

        return ResponseBuilder::Response(ResponseBuilder::buildSelectResponse(
            $response->data(),
            '',
            $this->domain,
            $count->data()
        ));
    }
}
