<?php

namespace App\Http\Middleware;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Closure;

use App\Models\Environment\Business;

class SetMaintenanceLang
{
    public function handle($request, Closure $next) {

        // Just Act on
        if (app()->isDownForMaintenance()) {
            try {
                $business = Business::select('language')
                    ->first();

                $lang = strtolower($business->language ?? config('app.locale'));
                App::setLocale($lang);
            } catch (\Throwable $e) {
                App::setLocale(config('app.locale'));
            }
        }

        return $next($request);
    }
}
