<?php

namespace App\Http\Middleware;

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

class ForceHttps
{
    public function handle(Request $request, Closure $next): Response
    {
        if (env('FORCE_HTTPS', false)) {
            $scheme = $request->headers->get('X-Forwarded-Proto', $request->getScheme());
            if ($scheme === 'https' && !$request->isSecure()) {
                return redirect()->secure($request->getRequestUri());
            }
        }

        return $next($request);
    }
}