<?php

namespace App\Core;

/**
 * Translation System (i18n)
 * يدعم الترجمة متعددة اللغات
 */
class Translation
{
    private static ?self $instance = null;
    private string $locale = 'ar';
    private string $fallbackLocale = 'ar';
    private array $translations = [];
    private string $translationsPath;

    private function __construct()
    {
        $this->translationsPath = __DIR__ . '/../../lang';
        $this->loadTranslations();
    }

    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * تحميل الترجمات
     */
    private function loadTranslations(): void
    {
        $localeFile = $this->translationsPath . '/' . $this->locale . '.php';
        if (file_exists($localeFile)) {
            $this->translations[$this->locale] = require $localeFile;
        }

        // تحميل fallback locale
        if ($this->locale !== $this->fallbackLocale) {
            $fallbackFile = $this->translationsPath . '/' . $this->fallbackLocale . '.php';
            if (file_exists($fallbackFile)) {
                $this->translations[$this->fallbackLocale] = require $fallbackFile;
            }
        }
    }

    /**
     * تعيين locale
     */
    public function setLocale(string $locale): void
    {
        if ($this->locale !== $locale) {
            $this->locale = $locale;
            $this->loadTranslations();
        }
    }

    /**
     * جلب locale الحالي
     */
    public function getLocale(): string
    {
        return $this->locale;
    }

    /**
     * ترجمة نص
     */
    public function trans(string $key, array $replace = []): string
    {
        $translation = $this->getTranslation($key);
        
        if (!empty($replace)) {
            foreach ($replace as $search => $value) {
                $translation = str_replace(':' . $search, $value, $translation);
            }
        }

        return $translation;
    }

    /**
     * جلب translation
     */
    private function getTranslation(string $key): string
    {
        // البحث في locale الحالي
        if (isset($this->translations[$this->locale][$key])) {
            return $this->translations[$this->locale][$key];
        }

        // البحث في fallback locale
        if ($this->locale !== $this->fallbackLocale && isset($this->translations[$this->fallbackLocale][$key])) {
            return $this->translations[$this->fallbackLocale][$key];
        }

        // إرجاع key إذا لم يتم العثور على translation
        return $key;
    }

    /**
     * ترجمة مع pluralization
     */
    public function transChoice(string $key, int $number, array $replace = []): string
    {
        $translation = $this->getTranslation($key);
        
        // دعم pluralization بسيط
        if (is_array($translation)) {
            if ($number === 1) {
                $translation = $translation['one'] ?? $translation[0] ?? $key;
            } else {
                $translation = $translation['other'] ?? $translation[1] ?? $translation[0] ?? $key;
            }
        }

        $translation = str_replace(':count', $number, $translation);
        
        if (!empty($replace)) {
            foreach ($replace as $search => $value) {
                $translation = str_replace(':' . $search, $value, $translation);
            }
        }

        return $translation;
    }

    /**
     * Helper function للاستخدام العام
     */
    public static function __(string $key, array $replace = []): string
    {
        return self::getInstance()->trans($key, $replace);
    }
}
