<?php

namespace App\Http\Middleware;

use App\Services\Client\BookingSessionService;
use App\Services\Client\CurrencyService;
use App\Services\Client\SearchSessionService;
use App\Services\SiteInfoService;
use Closure;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;

class ClientSharedMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {

        SearchSessionService::init();
        CurrencyService::init();
        BookingSessionService::init();

        // Share data with Inertia
        Inertia::share([
            'search' => fn() => SearchSessionService::get(),
            'booking' => fn() => BookingSessionService::get(),
            'currency' => fn() => CurrencyService::get(),
            'contact_details' => app(SiteInfoService::class)->get('contact_details'),
            'social_links' => app(SiteInfoService::class)->get('social_links'),
        ]);

        return $next($request);
    }
}
