<?php
namespace App\Controllers;

use App\Core\I18n;

class LanguageController
{
    public function set()
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }

        $lang = trim((string)($_GET['set'] ?? 'ar'));
        I18n::set($lang);

        $back = (string)($_SERVER['HTTP_REFERER'] ?? '/easychair/admin/dashboard');

        // Normalize accidental duplicated base path.
        $back = str_replace('/easychair/easychair/', '/easychair/', $back);

        // Keep redirects inside EasyChair only.
        if (!str_contains($back, '/easychair')) {
            $back = '/easychair/admin/dashboard';
        }

        header("Location: {$back}");
        exit;
    }
}
