<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class RootRedirectMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        // Only redirect if the path is exactly "/"
        if ($request->path() === '/') {
            $defaultLocale = \App\Helper\LocaleHelper::getDefaultLocale();
            return redirect('/' . $defaultLocale, 301); // 301 permanent redirect
        }

        return $next($request);
    }
}
