<?php
if (!defined('ABSPATH')) exit;

class DC_WA_Inquiry_By_XD_Core {

    private $loader;
    private $plugin_name;
    private $version;

    public function __construct() {
        try {
            $this->version = DC_WA_INQUIRY_BY_XD_VERSION;
            $this->plugin_name = 'dc-wa-inquiry-by-xd';
            
            $this->load_dependencies();
            $this->define_admin_hooks();
            $this->define_public_hooks();

        } catch (Exception $e) {
            add_action('admin_notices', function() use ($e) {
                echo '<div class="error notice is-dismissible"><p>' . 
                     esc_html__('Direct Chat Inquiry Error:', 'direct-chat-inquiry-by-xd') . ' ' . 
                     esc_html($e->getMessage()) . '</p></div>';
            });
        }
    }

    private function load_dependencies() {
        require_once DC_WA_INQUIRY_BY_XD_PLUGIN_DIR . 'includes/class-dc-wa-inquiry-by-xd-loader.php';
        require_once DC_WA_INQUIRY_BY_XD_PLUGIN_DIR . 'admin/class-dc-wa-inquiry-by-xd-admin.php';
        require_once DC_WA_INQUIRY_BY_XD_PLUGIN_DIR . 'public/class-dc-wa-inquiry-by-xd-public.php';
        
        $this->loader = new DC_WA_Inquiry_By_XD_Loader();
    }

    private function define_admin_hooks() {
        $plugin_admin = new DC_WA_Inquiry_By_XD_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_admin_menu');
        $this->loader->add_action('admin_init', $plugin_admin, 'settings_init');
    }

    private function define_public_hooks() {
        $plugin_public = new DC_WA_Inquiry_By_XD_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');
        $this->loader->add_action('init', $plugin_public, 'register_shortcodes');
        
        // Add button based on settings
        $position = get_option('dc_wa_inquiry_by_xd_button_position', 'after_add_to_cart');
        
        switch ($position) {
            case 'before_add_to_cart':
                $this->loader->add_action('woocommerce_before_add_to_cart_button', $plugin_public, 'display_inquiry_button');
                break;
            case 'after_summary':
                $this->loader->add_action('woocommerce_after_single_product_summary', $plugin_public, 'display_inquiry_button', 5);
                break;
            default: // 'after_add_to_cart'
                $this->loader->add_action('woocommerce_after_add_to_cart_button', $plugin_public, 'display_inquiry_button');
        }
    }

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

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

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