<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\RoomCategory;
use Inertia\Inertia;
use Laravel\Fortify\Features;

class HomeController extends Controller
{
    public function index()
    {

        $roomCategories = RoomCategory::query()
            ->with(['facilities:id,name,is_icon,icon_value', 'rooms:id,room_category_id'])
            ->orderBy('sort_order', 'asc')
            ->get(['id', 'name', 'short_name', 'slug', 'description', 'base_price', 'max_adults', 'max_children'])
            ->map(function ($category) {
                $category->setRelation('facilities',  collect($category->facilities->map(fn($f) =>  ['name' => $f->name,  'is_icon' => $f->is_icon,  'icon_value' => $f->icon_value])));
                $category->setRelation('rooms',  collect($category->rooms->map(fn($r) => ['url' => $r->thumbnail()?->url, 'alt_text' => $r->thumbnail()?->alt_text])));
                return $category;
            });

        return Inertia::render('home/welcome', [
            'canRegister' => Features::enabled(Features::registration()),
            'roomCategories' => $roomCategories,
            'maxAdults' => RoomCategory::getMaxAdultsAllowed(),
            'maxChildren' =>  RoomCategory::getMaxChildrenAllowed()
        ]);
    }
}
