<?php

namespace App\Http\Controllers\language;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class LanguageController extends Controller
{
    /**
     * Supported application locales.
     */
    public const SUPPORTED_LOCALES = [
        'en', 'ar', 'fr', 'es', 'de', 'it', 'pt', 'ru',
        'ja', 'ko', 'tr', 'id', 'hi', 'ta', 'bn', 'te', 'th', 'vi',
    ];

    /**
     * RTL (Right-to-Left) locales.
     */
    public const RTL_LOCALES = ['ar'];

    public function swap(Request $request, $locale)
    {
        if (! in_array($locale, self::SUPPORTED_LOCALES)) {
            abort(400);
        }

        $request->session()->put('locale', $locale);
        App::setLocale($locale);

        // Set RTL direction cookie based on language
        $isRtl = in_array($locale, self::RTL_LOCALES);
        $cookie = cookie('direction', $isRtl ? 'true' : 'false', 60 * 24 * 365); // 1 year

        return redirect()->back()->withCookie($cookie);
    }
}
