<?php

declare(strict_types=1);

namespace App\Http\Controllers\Filament;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AdminLocaleSwitchController extends Controller
{
    public function __invoke(Request $request, string $locale): RedirectResponse
    {
        /** @var array<string, string> $supported */
        $supported = config('dberp.admin.locales', []);

        if (! array_key_exists($locale, $supported)) {
            abort(Response::HTTP_NOT_FOUND);
        }

        $request->session()->put('dberp_admin_locale', $locale);

        return redirect()->back(fallback: route('filament.admin.auth.login'));
    }
}
