<?php

namespace App\Http\Controllers\Owner\RoomCalendar;

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

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

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

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

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

    public function richElement($element)
    {
        $element->PassengerList = DataUtils::decodeMysqlJson($element, 'PassengerList');
        $element->PassengerList = array_map(function ($pass) {
            $pass['UserCountry_Img_Path'] = $this->storageProvider->publicCountryPath($pass['UserCountry_Img']);
            return $pass;
        }, $element->PassengerList);
        return $element;
    }
}
