<?php
namespace EveAI\Chat;

class Plugin {
    /**
     * Plugin instance
     */
    private static $instance = null;

    /**
     * Plugin components
     */
    private $components = [];

    /**
     * Get plugin instance
     */
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Initialize plugin
     */
    private function __construct() {
        $this->load_dependencies();
        $this->init_components();
        $this->register_hooks();
    }

    /**
     * Load dependencies
     */
    private function load_dependencies() {
        // Core files
        require_once EVEAI_CHAT_PLUGIN_DIR . 'includes/interface-loadable.php';
        require_once EVEAI_CHAT_PLUGIN_DIR . 'includes/class-assets.php';
        require_once EVEAI_CHAT_PLUGIN_DIR . 'includes/class-shortcode.php';
        require_once EVEAI_CHAT_PLUGIN_DIR . 'includes/class-rest-controller.php';
        require_once EVEAI_CHAT_PLUGIN_DIR . 'includes/class-security.php';

        // Admin
        if (is_admin()) {
            require_once EVEAI_CHAT_PLUGIN_DIR . 'admin/class-admin.php';
        }
    }

    /**
     * Initialize components
     */
    private function init_components() {
        // Initialize REST controller
        $this->components['rest'] = new RESTController();

        // Initialize assets manager
        $this->components['assets'] = new Assets();

        // Initialize shortcode handler
        $this->components['shortcode'] = new Shortcode();

        // Initialize admin if in admin area
        if (is_admin()) {
            $this->components['admin'] = new Admin();
        }

        // Initialize all components
        foreach ($this->components as $component) {
            if ($component instanceof Loadable) {
                $component->init();
            }
        }
    }

    /**
     * Register WordPress hooks
     */
    private function register_hooks() {
        // Plugin activation/deactivation
        register_activation_hook(EVEAI_CHAT_PLUGIN_DIR . 'eveai-chat.php', [$this, 'activate']);
        register_deactivation_hook(EVEAI_CHAT_PLUGIN_DIR . 'eveai-chat.php', [$this, 'deactivate']);

        // Load text domain
        add_action('plugins_loaded', [$this, 'load_plugin_textdomain']);
    }

    /**
     * Plugin activation
     */
    public function activate() {
        // Set default options if not exists
        if (!get_option('eveai_chat_settings')) {
            add_option('eveai_chat_settings', [
                'auth_url' => 'https://api.askeveai.com',
                'socket_url' => 'https://chat.askeveai.com',
                'tenant_id' => '',
                'api_key' => ''
            ]);
        }

        // Clear permalinks
        flush_rewrite_rules();
    }

    /**
     * Plugin deactivation
     */
    public function deactivate() {
        // Clear any scheduled hooks, clean up temporary data, etc.
        flush_rewrite_rules();
    }

    /**
     * Load plugin textdomain
     */
    public function load_plugin_textdomain() {
        load_plugin_textdomain(
            'eveai-chat',
            false,
            dirname(plugin_basename(EVEAI_CHAT_PLUGIN_DIR)) . '/languages/'
        );
    }
}