<?php
class SeekMake {

    protected $loader;
    protected $plugin_name;
    protected $version;

    public function __construct() {
        $this->version = SEEKMAKE_VERSION;
        $this->plugin_name = 'seekmake';

        $this->load_dependencies();
        $this->define_admin_hooks();
        $this->define_public_hooks();
        $this->load_features();
    }

    private function load_dependencies() {
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/class-seekmake-loader.php';
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/class-seekmake-admin.php';
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/class-seekmake-public.php';
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/class-seekmake-security.php';
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/class-seekmake-event-handler.php';
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/class-seekmake-settings.php';

        $this->loader = new SeekMake_Loader();
    }

    private function define_admin_hooks() {
        $plugin_admin = new SeekMake_Admin($this->get_plugin_name(), $this->get_version());
        
        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
        $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
        $this->loader->add_action('admin_menu', $plugin_admin, 'add_plugin_admin_menu');
        $this->loader->add_action('admin_init', $plugin_admin, 'register_settings');

        $event_handler = new SeekMake_Event_Handler();
        $this->loader->add_action('wp_ajax_seekmake_handle_event', $event_handler, 'handle_event');
        $this->loader->add_action('wp_ajax_seekmake_save_settings', $event_handler, 'save_settings');
        $this->loader->add_action('wp_ajax_seekmake_get_settings', $event_handler, 'get_settings');
        $this->loader->add_action('wp_ajax_seekmake_handle_auth', $event_handler, 'handle_auth');
        $this->loader->add_action('wp_ajax_seekmake_disconnect_account', $event_handler, 'disconnect_account');
    }

    private function define_public_hooks() {
        $plugin_public = new SeekMake_Public($this->get_plugin_name(), $this->get_version());
        
        $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
        $this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
    }

    private function load_features() {
        $this->load_instant_quote_feature();
        $this->load_client_dashboard_feature();
        $this->load_astra_ai_feature();
        $this->load_woocommerce_feature();
    }
    
    private function load_instant_quote_feature() {
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/features/instant-quote/class-instant-quote.php';
        $instant_quote = new SeekMake_Instant_Quote();
        $instant_quote->init();
    }
    
    private function load_client_dashboard_feature() {
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/features/client-dashboard/class-client-dashboard.php';
        $client_dashboard = new SeekMake_Client_Dashboard();
        $client_dashboard->init();
    }

    private function load_astra_ai_feature() {
        require_once SEEKMAKE_PLUGIN_DIR . 'includes/features/astra-ai/class-astra-ai.php';
        $astra_ai = new SeekMake_Astra_AI();
        $astra_ai->init();
    }

    private function load_woocommerce_feature() {
        add_action('init', function() {
            if (get_option('seekmake_woocommerce_enabled', false) && class_exists('WooCommerce')) {
                require_once SEEKMAKE_PLUGIN_DIR . 'includes/features/woocommerce/class-seekmake-woocommerce.php';
                $woocommerce = new SeekMake_WooCommerce();
                $woocommerce->init();
            }
        }, 5);
    }

    public function run() {
        $this->loader->run();
    }

    public function get_plugin_name() {
        return $this->plugin_name;
    }

    public function get_loader() {
        return $this->loader;
    }

    public function get_version() {
        return $this->version;
    }
}
