<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @package SEO_Pro_Stack
 * @subpackage SEO_Pro_Stack/Admin
 */

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * The admin-specific functionality of the plugin.
 */
class SEOProStack_Admin {

    /**
     * Initialize the admin functionality.
     */
    public function initialize() {
        // Register settings page
        add_action('admin_menu', array($this, 'add_settings_page'));
        
        // Register admin assets
        add_action('admin_enqueue_scripts', array($this, 'enqueue_styles'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
        
        // Initialize AJAX handlers
        $this->init_ajax_handlers();
    }

    /**
     * Register the stylesheets for the admin area.
     *
     * @param string $hook The current admin page.
     */
    public function enqueue_styles($hook) {
        // Only load styles on our settings page
        if (strpos($hook, 'seoprostack') === false && strpos($hook, 'page=seoprostack') === false) {
            return;
        }
        
        wp_enqueue_style(
            'seoprostack-admin',
            SEOPROSTACK_PLUGIN_URL . 'admin/css/seoprostack-admin.css',
            array(),
            SEOPROSTACK_VERSION
        );
        
        wp_enqueue_style(
            'seoprostack-plugins',
            SEOPROSTACK_PLUGIN_URL . 'admin/css/seoprostack-plugins.css',
            array(),
            SEOPROSTACK_VERSION
        );
    }

    /**
     * Register the JavaScript for the admin area.
     *
     * @param string $hook The current admin page.
     */
    public function enqueue_scripts($hook) {
        // Only load scripts on our settings page
        if (strpos($hook, 'seoprostack') === false && strpos($hook, 'page=seoprostack') === false) {
            return;
        }
        
        // Enqueue WordPress updates script for theme installation
        wp_enqueue_script('updates');
        
        wp_enqueue_script(
            'seoprostack-admin',
            SEOPROSTACK_PLUGIN_URL . 'admin/js/seoprostack-admin.js',
            array('jquery', 'updates'),
            SEOPROSTACK_VERSION,
            true
        );
        
        // Localize script for AJAX
        $ajax_data = array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'adminUrl' => admin_url(),
            'nonce' => wp_create_nonce('seoprostack_ajax_nonce'),
            'updateNonce' => wp_create_nonce('updates')
        );
        
        wp_localize_script('seoprostack-admin', 'seoProStack', $ajax_data);
    }

    /**
     * Add settings page to the WordPress admin menu.
     */
    public function add_settings_page() {
        // Initialize settings page
        require_once SEOPROSTACK_PLUGIN_DIR . 'admin/settings/class-seoprostack-settings-page.php';
        $settings_page = new SEOProStack_Settings_Page();
        $settings_page->add_menu_page();
        $settings_page->register_settings();
    }

    /**
     * Initialize AJAX handlers.
     */
    public function init_ajax_handlers() {
        // Pro Plugins AJAX handler
        require_once SEOPROSTACK_PLUGIN_DIR . 'admin/settings/ajax/class-seoprostack-ajax-pro-plugins.php';
        $pro_plugins_ajax = new SEOProStack_AJAX_Pro_Plugins();
        $pro_plugins_ajax->init();
        
        // Advanced Settings AJAX handler
        require_once SEOPROSTACK_PLUGIN_DIR . 'admin/settings/ajax/class-seoprostack-ajax-advanced.php';
        $advanced_ajax = new SEOProStack_AJAX_Advanced();
        $advanced_ajax->init();
        
        // Tools AJAX handler
        require_once SEOPROSTACK_PLUGIN_DIR . 'admin/settings/ajax/class-seoprostack-ajax-tools.php';
        $tools_ajax = new SEOProStack_AJAX_Tools();
        $tools_ajax->init();
    }
}