<?php

namespace App\Http\Middleware;

use App\Services\TenantContext;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use App\Models\Hotel;

class HotelContext
{
    public function handle(Request $request, Closure $next)
    {
        if (Auth::check()) {
            $hotelId = app(TenantContext::class)->hotelId();

            if ($hotelId) {
                session(['current_hotel_id' => (int) $hotelId]);

                $currentHotel = Hotel::find((int) $hotelId);
                View::share('currentHotel', $currentHotel);
            }
        }

        return $next($request);
    }
}