<?php
/**
 * Internationalization functionality for BoostBot.
 *
 * @package BoostBot
 */

// Prevent direct access to the file.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * BoostBot_i18n class.
 *
 * Handles plugin internationalization, including text domain loading,
 * RTL support, and translation plugin compatibility.
 *
 * @since 1.0.0
 */
class BoostBot_i18n
{
    /**
     * The text domain for the plugin.
     *
     * @since 1.0.0
     * @access private
     * @var string
     */
    private $text_domain = 'boostbot';

    /**
     * Singleton instance.
     *
     * @since 1.0.0
     * @access private static
     * @var BoostBot_i18n|null
     */
    private static $instance = null;

    /**
     * Get the singleton instance.
     *
     * @since 1.0.0
     * @return BoostBot_i18n
     */
    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor.
     *
     * @since 1.0.0
     */
    private function __construct()
    {
        // Private constructor for singleton
    }

    /**
     * Initialize i18n functionality.
     *
     * @since 1.0.0
     */
    public function init()
    {
        // Load plugin text domain
        add_action('init', [$this, 'load_textdomain']);

        // Register strings with translation plugins
        add_action('init', [$this, 'register_translation_strings'], 20);

        // Add RTL body class
        add_filter('body_class', [$this, 'add_rtl_body_class']);
        add_filter('admin_body_class', [$this, 'add_rtl_admin_body_class']);
    }

    /**
     * Load the plugin text domain.
     *
     * @since 1.0.0
     */
    public function load_textdomain()
    {
        /**
         * WordPress 4.6+ handles loading text domain automatically from WordPress.org.
         * Keeping the method for backward compatibility.
         */

        if (class_exists('BoostBot_Logger')) {
            BoostBot_Logger::debug('Text domain loaded', [
                'domain' => $this->text_domain,
                'locale' => get_locale(),
                'is_rtl' => is_rtl(),
            ]);
        }
    }

    /**
     * Register strings with WPML and Polylang.
     *
     * @since 1.0.0
     */
    public function register_translation_strings()
    {
        // Get configurable strings that users might want to translate
        $strings = $this->get_translatable_options();

        // Register with WPML
        if (function_exists('icl_register_string')) {
            foreach ($strings as $name => $value) {
                icl_register_string('boostbot', $name, $value);
            }
        }

        // Register with Polylang
        if (function_exists('pll_register_string')) {
            foreach ($strings as $name => $value) {
                pll_register_string($name, $value, 'BoostBot', true);
            }
        }
    }

    /**
     * Get translatable option strings.
     *
     * @since 1.0.0
     * @return array Translatable strings.
     */
    private function get_translatable_options()
    {
        return [
            'boostbot_welcome_message' => BoostBot_Settings::get('welcome_message', 'Hello! How can I help you?'),
            'boostbot_bot_name'        => BoostBot_Settings::get('bot_name', 'BoostBot'),
        ];
    }

    /**
     * Get translated option value.
     *
     * @since 1.0.0
     * @param string $option_name Option name.
     * @param string $default     Default value.
     * @return string Translated value.
     */
    public function get_translated_option($option_name, $default = '')
    {
        $value = BoostBot_Settings::get($option_name, $default);

        // Try WPML first
        if (function_exists('icl_t')) {
            return icl_t('boostbot', 'boostbot_' . $option_name, $value);
        }

        // Try Polylang
        if (function_exists('pll__')) {
            return pll__($value);
        }

        return $value;
    }

    /**
     * Add RTL body class for frontend.
     *
     * @since 1.0.0
     * @param array $classes Body classes.
     * @return array Modified body classes.
     */
    public function add_rtl_body_class($classes)
    {
        if (is_rtl()) {
            $classes[] = 'boostbot-rtl';
        }
        return $classes;
    }

    /**
     * Add RTL body class for admin.
     *
     * @since 1.0.0
     * @param string $classes Admin body classes.
     * @return string Modified admin body classes.
     */
    public function add_rtl_admin_body_class($classes)
    {
        if (is_rtl()) {
            $classes .= ' boostbot-rtl';
        }
        return $classes;
    }

    /**
     * Get direction configuration for JavaScript.
     *
     * @since 1.0.0
     * @return array Direction configuration.
     */
    public function get_direction_config()
    {
        return [
            'isRtl'         => is_rtl(),
            'direction'     => is_rtl() ? 'rtl' : 'ltr',
            'locale'        => get_locale(),
            'language'      => $this->get_current_language(),
            'textDirection' => is_rtl() ? 'rtl' : 'ltr',
        ];
    }

    /**
     * Get current language code.
     *
     * @since 1.0.0
     * @return string Language code.
     */
    public function get_current_language()
    {
        // WPML
        if (defined('ICL_LANGUAGE_CODE')) {
            return ICL_LANGUAGE_CODE;
        }

        // Polylang
        if (function_exists('pll_current_language')) {
            return pll_current_language();
        }

        // WordPress default
        return substr(get_locale(), 0, 2);
    }

    /**
     * Check if current language is Hebrew.
     *
     * @since 1.0.0
     * @return bool True if Hebrew.
     */
    public function is_hebrew()
    {
        $locale = get_locale();
        return strpos($locale, 'he_') === 0 || $locale === 'he';
    }

    /**
     * Get all UI strings for JavaScript.
     *
     * @since 1.0.0
     * @return array UI strings.
     */
    public function get_js_strings()
    {
        return [
            // Navigation
            'dashboard'     => __('Dashboard', 'boostbot'),
            'conversations' => __('Conversations', 'boostbot'),
            'leads'         => __('Leads', 'boostbot'),
            'settings'      => __('Settings', 'boostbot'),
            'analytics'     => __('Analytics', 'boostbot'),
            'widgets'       => __('Widgets', 'boostbot'),

            // Actions
            'save'          => __('Save', 'boostbot'),
            'cancel'        => __('Cancel', 'boostbot'),
            'delete'        => __('Delete', 'boostbot'),
            'edit'          => __('Edit', 'boostbot'),
            'export'        => __('Export', 'boostbot'),
            'search'        => __('Search', 'boostbot'),
            'filter'        => __('Filter', 'boostbot'),
            'refresh'       => __('Refresh', 'boostbot'),

            // Status
            'active'        => __('Active', 'boostbot'),
            'inactive'      => __('Inactive', 'boostbot'),
            'loading'       => __('Loading...', 'boostbot'),
            'error'         => __('Error', 'boostbot'),
            'success'       => __('Success', 'boostbot'),

            // Messages
            'saved'         => __('Settings saved successfully', 'boostbot'),
            'error_generic' => __('An error occurred. Please try again.', 'boostbot'),
            'confirm_delete'=> __('Are you sure you want to delete this item?', 'boostbot'),
            'no_results'    => __('No results found', 'boostbot'),

            // License
            'free'          => __('Free', 'boostbot'),
            'pro'           => __('Pro', 'boostbot'),
            'upgrade'       => __('Upgrade to Pro', 'boostbot'),
            'expired'       => __('Expired', 'boostbot'),

            // Chat
            'type_message'  => __('Type a message...', 'boostbot'),
            'send'          => __('Send', 'boostbot'),
            'thinking'      => __('Thinking...', 'boostbot'),
            'new_chat'      => __('New Conversation', 'boostbot'),

            // Leads
            'new_lead'      => __('New', 'boostbot'),
            'contacted'     => __('Contacted', 'boostbot'),
            'qualified'     => __('Qualified', 'boostbot'),
            'converted'     => __('Converted', 'boostbot'),
            'lost'          => __('Lost', 'boostbot'),

            // Time
            'today'         => __('Today', 'boostbot'),
            'yesterday'     => __('Yesterday', 'boostbot'),
            'this_week'     => __('This week', 'boostbot'),
            'this_month'    => __('This month', 'boostbot'),

            // Validation
            'required'      => __('This field is required', 'boostbot'),
            'invalid_email' => __('Please enter a valid email address', 'boostbot'),
            'invalid_phone' => __('Please enter a valid phone number', 'boostbot'),
        ];
    }

    /**
     * Get the text domain.
     *
     * @since 1.0.0
     * @return string Text domain.
     */
    public function get_text_domain()
    {
        return $this->text_domain;
    }
}










