<?php

namespace App\Http\Controllers\Owner\RoomCalendar;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Owner\RoomCalendar\Dto\RoomCalendarListDTO;
use App\Http\Helpers\ResponseBuilder;
use Illuminate\Http\Request;
use App\Modules\Shared\Domain\Bus\Query\QueryBus;
use App\Modules\Shared\Domain\DataUtils;
use App\Modules\Shared\Validator\Infraestructure\LaravelDTOGenerator;
use App\Modules\Room\Application\RoomResponses;
use App\Modules\RoomCalendar\Application\List\ListRoomCalendarQuery;
use App\Providers\StorageProvider;

class RoomCalendarListController extends Controller
{
    private $domain = 'Room';
    public function __construct(
        private QueryBus $queryBus,
        private StorageProvider $storageProvider,
        private LaravelDTOGenerator $dtoGenerator,
    ) {}

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

        /** @var RoomResponses $response */
        $response = $this->queryBus->ask(new ListRoomCalendarQuery(
            $dto->DateStart,
            $dto->DateEnd,
        ));

        return ResponseBuilder::Response(ResponseBuilder::buildSelectResponse(
            array_map([$this, 'richElement'], $response->data()),
            '',
            $this->domain
        ));
    }

    public function richElement($element)
    {
        $element->PassengerList = DataUtils::decodeMysqlJson($element, 'PassengerList');
        return $element;
    }
}
