<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LocaleController extends Controller
{
    public function switch(string $locale)
    {
        $available = ['en', 'bn', 'ar'];

        if (! in_array($locale, $available, true)) {
            abort(404);
        }

        // Persist to the authenticated user's record
        if (Auth::check()) {
            Auth::user()->update(['locale' => $locale]);
        }

        session(['locale' => $locale]);
        app()->setLocale($locale);

        return redirect()->back();
    }
}
