<?php

class Lang
{
    private static array $translations = [];
    private static string $locale = 'en';

    public static function init(): void
    {
        // First check if user has manually selected a language in this session
        if (Session::get('locale')) {
            self::$locale = Session::get('locale');
        }
        // Then check if user is logged in and has a preferred language
        elseif (Session::isLoggedIn()) {
            require_once APP_ROOT . '/models/User.php';
            $userModel = new User();
            $user = $userModel->findById(Session::getUserId());
            if ($user && !empty($user['default_language'])) {
                self::$locale = $user['default_language'];
            } else {
                self::$locale = 'en'; // Default to English for new users
            }
        }
        // Fallback to session or English
        else {
            self::$locale = Session::get('default_language', 'en');
        }
        
        self::load();
    }

    public static function load(): void
    {
        // Always load English as the fallback base
        $enFile = APP_ROOT . '/lang/en.php';
        if (file_exists($enFile)) {
            self::$translations = require $enFile;
        }

        // Overlay selected language on top (missing keys fall back to English)
        if (self::$locale !== 'en') {
            $file = APP_ROOT . '/lang/' . self::$locale . '.php';
            if (file_exists($file)) {
                $localeTranslations = require $file;
                self::$translations = array_merge(self::$translations, $localeTranslations);
            }
        }
    }

    public static function get(string $key, string $default = ''): string
    {
        return self::$translations[$key] ?? ($default ?: $key);
    }

    public static function getLocale(): string
    {
        return self::$locale;
    }

    public static function getHtmlLocale(): string
    {
        return self::hreflangCode(self::$locale);
    }

    public static function supportedLocales(): array
    {
        return ['en', 'sr', 'de', 'fr', 'es', 'it', 'ru', 'pt', 'nl', 'pl', 'cs', 'tr', 'hu', 'ro', 'el', 'no', 'sv', 'jp', 'cn', 'il'];
    }

    public static function hreflangCode(string $locale): string
    {
        return match ($locale) {
            'cn' => 'zh',
            'jp' => 'ja',
            'il' => 'he',
            default => $locale,
        };
    }

    public static function setLocale(string $locale): void
    {
        self::$locale = $locale;
        Session::set('locale', $locale);
        self::load();
    }

    public static function isSr(): bool
    {
        return self::$locale === 'sr';
    }

    public static function isEn(): bool
    {
        return self::$locale === 'en';
    }

    public static function isCs(): bool
    {
        return self::$locale === 'cs';
    }

    public static function isCn(): bool
    {
        return self::$locale === 'cn';
    }

    public static function isDe(): bool
    {
        return self::$locale === 'de';
    }

    public static function isEl(): bool
    {
        return self::$locale === 'el';
    }

    public static function isEs(): bool
    {
        return self::$locale === 'es';
    }

    public static function isFr(): bool
    {
        return self::$locale === 'fr';
    }

    public static function isHu(): bool
    {
        return self::$locale === 'hu';
    }

    public static function isIl(): bool
    {
        return self::$locale === 'il';
    }

    public static function isIt(): bool
    {
        return self::$locale === 'it';
    }

    public static function isJp(): bool
    {
        return self::$locale === 'jp';
    }

    public static function isNl(): bool
    {
        return self::$locale === 'nl';
    }

    public static function isNo(): bool
    {
        return self::$locale === 'no';
    }

    public static function isPt(): bool
    {
        return self::$locale === 'pt';
    }

    public static function isRo(): bool
    {
        return self::$locale === 'ro';
    }

    public static function isRu(): bool
    {
        return self::$locale === 'ru';
    }

    public static function isSv(): bool
    {
        return self::$locale === 'sv';
    }

    public static function isTr(): bool
    {
        return self::$locale === 'tr';
    }

    public static function isPl(): bool
    {
        return self::$locale === 'pl';
    }

    public static function getCurrentFlag(): string
    {
        return match(self::$locale) {
            'en' => 'EN',
            'cs' => 'CZ',
            'cn' => 'CN',
            'de' => 'DE',
            'el' => 'GR',
            'es' => 'ES',
            'fr' => 'FR',
            'hu' => 'HU',
            'il' => 'IL',
            'it' => 'IT',
            'jp' => 'JP',
            'nl' => 'NL',
            'no' => 'NO',
            'pt' => 'PT',
            'ro' => 'RO',
            'ru' => 'RU',
            'sr' => 'RS',
            'sv' => 'SE',
            'tr' => 'TR',
            'pl' => 'PL',
            default => 'EN'
        };
    }

    public static function getCurrentFlagEmoji(): string
    {
        return match(self::$locale) {
            'en' => 'EN',
            'cs' => 'CZ',
            'cn' => 'CN',
            'de' => 'DE',
            'el' => 'GR',
            'es' => 'ES',
            'fr' => 'FR',
            'hu' => 'HU',
            'il' => 'IL',
            'it' => 'IT',
            'jp' => 'JP',
            'nl' => 'NL',
            'no' => 'NO',
            'pt' => 'PT',
            'ro' => 'RO',
            'ru' => 'RU',
            'sr' => 'RS',
            'sv' => 'SE',
            'tr' => 'TR',
            'pl' => 'PL',
            default => 'EN'
        };
    }
}
