<?php
// src/Helpers/Language.php

require_once __DIR__ . '/../../config/config.php';

class Language {
    private static ?array $translations = null;

    public static function init(): void {
        if (isset($_GET['lang']) && in_array($_GET['lang'], ['en', 'ar'])) {
            $_SESSION['lang'] = $_GET['lang'];
        }
        if (!isset($_SESSION['lang'])) {
            $_SESSION['lang'] = 'en';
        }

        if (isset($_GET['currency']) && in_array($_GET['currency'], ['NGN', 'USD'])) {
            $_SESSION['currency'] = $_GET['currency'];
        }
        if (!isset($_SESSION['currency'])) {
            $_SESSION['currency'] = 'NGN';
        }

        if (self::$translations === null) {
            self::$translations = require __DIR__ . '/../../config/translations.php';
        }
    }

    public static function current(): string {
        self::init();
        return $_SESSION['lang'];
    }

    public static function isRtl(): bool {
        return self::current() === 'ar';
    }

    public static function dir(): string {
        return self::isRtl() ? 'rtl' : 'ltr';
    }

    public static function currency(): string {
        self::init();
        return $_SESSION['currency'];
    }

    public static function currencySymbol(): string {
        return self::currency() === 'NGN' ? '₦' : '$';
    }

    public static function t(string $key): string {
        self::init();
        $lang = self::current();
        return self::$translations[$lang][$key] ?? $key;
    }
}
