<?php
class kavehboardassets {
    public $dashboard_page_id;

    public function __construct() {        
        // Get options directly from WordPress options table
        $options = get_option('kavehboard_options');
        // Extract page IDs from the options array
        $this->dashboard_page_id = isset($options['kavehboard_page']) ? $options['kavehboard_page'] : '';
        add_action('wp', array($this, 'setup_scripts'));
    }

    public function setup_scripts() {
        // Only hook into wp_enqueue_scripts if we're on our special pages
        if (is_page($this->dashboard_page_id)) {
            add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts_and_styles'),999999999);
        }
    }

    public function enqueue_scripts_and_styles() {
        // Register JS files
        wp_register_script('kavehboard-dashboard', KAVEHBOARD_URL . 'assets/js/script-dashboard.js', array('jquery'), KAVEHBOARD_VERSION, true);
        wp_register_script('kavehboard-notifications', KAVEHBOARD_URL . 'assets/js/notifications.js', array('jquery'), KAVEHBOARD_VERSION, true);

        $scripts = array(
            'script-dashboard.js' => 'assets/js/script-dashboard.js',
            'wallet.js' => 'assets/js/wallet.js',
        );

        $styles = array(
            'dashboard.min.css' => 'assets/css/dashboard.min.css',
            'notifications.css' => 'assets/css/notifications.css',
            'wallet.css' => 'assets/css/wallet.css',
        );

        foreach ($scripts as $handle => $src) {
            wp_enqueue_script($handle, KAVEHBOARD_URL . $src, array('jquery'), KAVEHBOARD_VERSION, true);
        }

        foreach ($styles as $handle => $src) {
            wp_enqueue_style($handle, KAVEHBOARD_URL . $src, false, KAVEHBOARD_VERSION, 'all');
        }

        // Enqueue dashboard scripts and styles
        if (is_page($this->dashboard_page_id)) {
            wp_enqueue_style('kavehboard-notifications');
            wp_enqueue_script('kavehboard-notifications');
            
            // Localize script for AJAX
            wp_localize_script('kavehboard-notifications', 'kavehboard_ajax', array(
                'ajax_url' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('kavehboard_nonce')
            ));
            
            // Localize wallet scripts
            wp_localize_script('wallet.js', 'kaveh_wallet_ajax', array(
                'ajax_url' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('kaveh_wallet_nonce'),
                'currency_symbol' => get_woocommerce_currency_symbol(),
                'currency_position' => get_option('woocommerce_currency_pos'),
                'thousand_separator' => wc_get_price_thousand_separator(),
                'decimal_separator' => wc_get_price_decimal_separator(),
                'decimals' => wc_get_price_decimals()
            ));
            
            // Enqueue wallet scripts on checkout page
            if (is_checkout()) {
                wp_enqueue_script('wallet-checkout.js');
                wp_localize_script('wallet-checkout.js', 'kaveh_wallet_checkout', array(
                    'ajax_url' => admin_url('admin-ajax.php'),
                    'nonce' => wp_create_nonce('kaveh_wallet_nonce'),
                    'currency_symbol' => get_woocommerce_currency_symbol(),
                    'currency_position' => get_option('woocommerce_currency_pos')
                ));
            }
        }

        // Check if current page matches any of our special pages
        if (is_page($this->dashboard_page_id)) {
            wp_dequeue_style('astra-theme-css');
            wp_dequeue_style('astra-addon-css');
            wp_dequeue_style('ct-main-styles');
        }
    }
}
