<?php
/**
 * Hook Registry — centralises all WordPress hook registration for WiseWand.
 *
 * Extracted from Plugin.php to give Plugin a single responsibility (bootstrap)
 * and keep hook wiring in one dedicated, auditable class.
 *
 * @package Wisewand
 * @since   2.8.2
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

namespace Wisewand\Core;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log -- error_log() is only used within WP_DEBUG conditionals for debugging

/**
 * Hook Registry
 *
 * Registers every WordPress action and filter on behalf of the WiseWand
 * plugin. Receives fully-constructed component instances via its constructor
 * so that each init_*_hooks() method can wire callbacks without needing to
 * know how components are created.
 *
 * @package Wisewand
 * @since   2.8.2
 */
class Hook_Registry {

	/**
	 * Main Plugin instance (provides public callback methods).
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Core\Plugin
	 */
	private $plugin;

	/**
	 * Admin Manager (provides admin pages and their AJAX methods).
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Admin\Admin_Manager|null
	 */
	private $admin_manager;

	/**
	 * Security Manager.
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Security\Security_Manager|null
	 */
	private $security_manager;

	/**
	 * REST API Controller.
	 *
	 * @since 2.8.2
	 * @var \Wisewand\API\REST_Controller|null
	 */
	private $rest_controller;

	/**
	 * Content Manager.
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Content\Content_Manager|null
	 */
	private $content_manager;

	/**
	 * Migration Validator (v2.8.0).
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Migration\Migration_Validator|null
	 */
	private $migration_validator;

	/**
	 * Migration Executor (v2.8.0).
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Migration\Migration_Executor|null
	 */
	private $migration_executor;

	/**
	 * Options wrapper (needed for Webhook_Receiver instantiation).
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Utils\Options|null
	 */
	private $options;

	/**
	 * Logger (needed for Webhook_Receiver instantiation).
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Logging\Logger|null
	 */
	private $logger;

	/**
	 * Widget Editor Support (v2.8.0).
	 *
	 * @since 2.8.2
	 * @var \Wisewand\Editor\Widget_Editor_Support|null
	 */
	private $widget_editor_support;

	/**
	 * CRON Scheduler (v2.8.2).
	 *
	 * @since 2.8.4
	 * @var \Wisewand\Core\CRON_Scheduler|null
	 */
	private $cron_scheduler;

	/**
	 * Constructor — stores every component reference needed for hook wiring.
	 *
	 * All parameters are accepted as mixed (no type-hints) to avoid fatal
	 * errors when optional components failed to initialise.
	 *
	 * @since 2.8.2
	 *
	 * @param \Wisewand\Core\Plugin                         $plugin               Main plugin instance.
	 * @param \Wisewand\Admin\Admin_Manager|null            $admin_manager        Admin manager instance.
	 * @param \Wisewand\Security\Security_Manager|null      $security_manager     Security manager instance.
	 * @param \Wisewand\API\REST_Controller|null            $rest_controller      REST controller instance.
	 * @param \Wisewand\Content\Content_Manager|null        $content_manager      Content manager instance.
	 * @param \Wisewand\Migration\Migration_Validator|null  $migration_validator  Migration validator instance.
	 * @param \Wisewand\Migration\Migration_Executor|null   $migration_executor   Migration executor instance.
	 * @param \Wisewand\Utils\Options|null                  $options              Options wrapper instance.
	 * @param \Wisewand\Logging\Logger|null                 $logger               Logger instance.
	 * @param \Wisewand\Editor\Widget_Editor_Support|null   $widget_editor_support Widget editor support instance.
	 * @param \Wisewand\Core\CRON_Scheduler|null             $cron_scheduler        CRON scheduler instance.
	 */
	public function __construct(
		$plugin,
		$admin_manager,
		$security_manager,
		$rest_controller,
		$content_manager,
		$migration_validator,
		$migration_executor,
		$options,
		$logger,
		$widget_editor_support,
		$cron_scheduler = null
	) {
		$this->plugin               = $plugin;
		$this->admin_manager        = $admin_manager;
		$this->security_manager     = $security_manager;
		$this->rest_controller      = $rest_controller;
		$this->content_manager      = $content_manager;
		$this->migration_validator  = $migration_validator;
		$this->migration_executor   = $migration_executor;
		$this->options              = $options;
		$this->logger               = $logger;
		$this->widget_editor_support = $widget_editor_support;
		$this->cron_scheduler       = $cron_scheduler;
	}

	/**
	 * Register all WordPress hooks.
	 *
	 * Orchestrates hook registration by delegating to focused methods grouped
	 * by functional category.
	 *
	 * @since 2.8.2
	 */
	public function init_hooks() {
		$this->init_admin_hooks();
		$this->init_ajax_hooks();
		$this->init_cron_hooks();
		$this->init_rest_api_hooks();
		$this->init_content_hooks();
		$this->init_settings_hooks();
		$this->init_diagnostic_hooks();
		$this->init_editor_hooks();
	}

	// =========================================================================
	// Admin hooks
	// =========================================================================

	/**
	 * Register admin interface hooks.
	 *
	 * Covers admin menu, admin_init, asset enqueueing, notices, security
	 * checks, and plugin version verification.
	 *
	 * @since 2.8.2
	 */
	private function init_admin_hooks() {
		// Database health check on admin_init.
		add_action( 'admin_init', array( $this->plugin, 'check_database_health' ), 5 );

		// v2.8.0: Migration system hooks.
		if ( $this->migration_validator ) {
			add_action( 'admin_notices', array( $this->plugin, 'show_migration_validation_notice' ) );
		}

		// Admin interface.
		if ( $this->admin_manager ) {
			add_action( 'admin_menu', array( $this->admin_manager, 'add_admin_pages' ) );
			add_action( 'admin_init', array( $this->admin_manager, 'init_settings' ) );
			add_action( 'admin_init', array( $this->plugin, 'handle_activation_redirect' ) );
			add_action( 'admin_notices', array( $this->plugin, 'show_activation_notice' ) );
			add_action( 'admin_enqueue_scripts', array( $this->admin_manager, 'enqueue_admin_assets' ) );
		}

		// Security checks.
		if ( $this->security_manager ) {
			add_action( 'init', array( $this->security_manager, 'init_security_checks' ) );
		}

		// Plugin version check.
		add_action( 'wp_loaded', array( $this->plugin, 'check_plugin_updates' ) );
	}

	// =========================================================================
	// AJAX hooks — dispatcher
	// =========================================================================

	/**
	 * Register all AJAX endpoint hooks.
	 *
	 * Delegates to sub-methods grouped by functional area:
	 * core admin, API settings, articles, projects, SEO intelligence,
	 * saved keywords, editorial calendar, deduplication, and RSS feeds.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_hooks() {
		if ( ! $this->admin_manager ) {
			return;
		}

		// Core admin AJAX handlers.
		add_action( 'wp_ajax_wisewand_dismiss_activation_notice', array( $this->plugin, 'dismiss_activation_notice' ) );
		add_action( 'wp_ajax_wisewand_test_connection', array( $this->admin_manager, 'ajax_test_connection' ) );
		add_action( 'wp_ajax_wisewand_pull_article', array( $this->admin_manager, 'ajax_pull_article' ) );
		add_action( 'wp_ajax_wisewand_export_logs', array( $this->admin_manager, 'ajax_export_logs' ) );
		add_action( 'wp_ajax_wisewand_purge_logs', array( $this->admin_manager, 'ajax_purge_logs' ) );

		$this->init_ajax_api_settings_hooks();
		$this->init_ajax_articles_hooks();
		$this->init_ajax_projects_hooks();
		$this->init_ajax_seo_intelligence_hooks();
		$this->init_ajax_saved_keywords_hooks();
		$this->init_ajax_editorial_calendar_hooks();
		$this->init_ajax_deduplication_hooks();
		$this->init_ajax_rss_feeds_hooks();
	}

	// =========================================================================
	// AJAX hooks — API settings
	// =========================================================================

	/**
	 * Register API settings AJAX hooks.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_api_settings_hooks() {
		add_action(
			'wp_ajax_wisewand_test_api_connection',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_api_settings_page' ) ) {
					$api_page = $this->admin_manager->get_api_settings_page();
					if ( $api_page ) {
						$api_page->ajax_test_api_connection();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_detailed_api_test',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_api_settings_page' ) ) {
					$api_page = $this->admin_manager->get_api_settings_page();
					if ( $api_page ) {
						$api_page->ajax_detailed_api_test();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_remove_api_key',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_api_settings_page' ) ) {
					$api_page = $this->admin_manager->get_api_settings_page();
					if ( $api_page ) {
						$api_page->ajax_remove_api_key();
					}
				}
			}
		);
	}

	// =========================================================================
	// AJAX hooks — Articles & generation
	// =========================================================================

	/**
	 * Register articles and content generation AJAX hooks.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_articles_hooks() {
		add_action(
			'wp_ajax_wisewand_import_article',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_articles_page' ) ) {
					$articles_page = $this->admin_manager->get_articles_page();
					if ( $articles_page ) {
						$articles_page->ajax_import_article();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_run_generation',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_articles_page' ) ) {
					$articles_page = $this->admin_manager->get_articles_page();
					if ( $articles_page ) {
						$articles_page->ajax_run_generation();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_bulk_import',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_articles_page' ) ) {
					$articles_page = $this->admin_manager->get_articles_page();
					if ( $articles_page ) {
						$articles_page->ajax_bulk_import();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_bulk_generate',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_articles_page' ) ) {
					$articles_page = $this->admin_manager->get_articles_page();
					if ( $articles_page ) {
						$articles_page->ajax_bulk_generate();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_test_import_workflow',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_articles_page' ) ) {
					$articles_page = $this->admin_manager->get_articles_page();
					if ( $articles_page ) {
						$articles_page->ajax_test_import_workflow();
					}
				}
			}
		);
	}

	// =========================================================================
	// AJAX hooks — Projects & personas
	// =========================================================================

	/**
	 * Register projects and personas AJAX hooks.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_projects_hooks() {
		add_action(
			'wp_ajax_wisewand_get_project_details',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_projects_page' ) ) {
					$projects_page = $this->admin_manager->get_projects_page();
					if ( $projects_page ) {
						$projects_page->ajax_get_project_details();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_persona_details',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_projects_page' ) ) {
					$projects_page = $this->admin_manager->get_projects_page();
					if ( $projects_page ) {
						$projects_page->ajax_get_persona_details();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_delete_persona',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_projects_page' ) ) {
					$projects_page = $this->admin_manager->get_projects_page();
					if ( $projects_page ) {
						$projects_page->ajax_delete_persona();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_debug_projects',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'ajax_debug_projects' ) ) {
					$this->admin_manager->ajax_debug_projects();
				}
			}
		);
	}

	// =========================================================================
	// AJAX hooks — SEO Intelligence
	// =========================================================================

	/**
	 * Register SEO Intelligence AJAX hooks.
	 *
	 * Includes DataForSEO debug handlers and real analysis handlers.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_seo_intelligence_hooks() {
		// DataForSEO balance check.
		add_action(
			'wp_ajax_wisewand_get_dataforseo_balance',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_get_dataforseo_balance();
					}
				}
			}
		);

		// SEO Intelligence Real AJAX handlers.
		add_action(
			'wp_ajax_wisewand_run_real_analysis',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_run_real_analysis();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_load_latest_snapshot',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_load_latest_snapshot();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_schedule_keyword',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_schedule_keyword();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_discover_keywords',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_discover_keywords();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_add_manual_keywords',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_add_manual_keywords();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_generate_smart_roadmap',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_generate_smart_roadmap();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_available_languages',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_get_available_languages();
					}
				}
			}
		);
	}

	// =========================================================================
	// AJAX hooks — Saved keywords
	// =========================================================================

	/**
	 * Register saved keywords AJAX hooks.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_saved_keywords_hooks() {
		add_action(
			'wp_ajax_wisewand_save_keywords_to_library',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_save_keywords_to_library();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_enrich_saved_keywords',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_enrich_saved_keywords();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_schedule_saved_keyword',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_schedule_saved_keyword();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_schedule_saved_keywords',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_schedule_saved_keywords();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_delete_saved_keyword',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_delete_saved_keyword();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_bulk_delete_saved_keywords',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_bulk_delete_saved_keywords();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_keyword_ideas',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_get_keyword_ideas();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_spy_competitors',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_spy_competitors();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_save_spy_keywords',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_save_spy_keywords();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_save_column_preferences',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_save_column_preferences();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_remove_from_calendar',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_remove_from_calendar();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_clear_all_calendar',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_clear_all_calendar();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_update_saved_keyword',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_update_saved_keyword();
					}
				}
			}
		);
	}

	// =========================================================================
	// AJAX hooks — Editorial calendar
	// =========================================================================

	/**
	 * Register editorial calendar AJAX hooks.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_editorial_calendar_hooks() {
		add_action(
			'wp_ajax_wisewand_get_calendar_events',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_get_calendar_events();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_entry_details',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_get_entry_details();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_update_entry_date',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_update_entry_gmdate();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_trigger_manual_cron',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_trigger_manual_cron();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_save_entry',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_save_entry();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_delete_entry',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_delete_entry();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_generate_instructions_template',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_generate_instructions_template();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_generate_instructions_ai',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_generate_instructions_ai();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_projects',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_get_projects();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_personas',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_get_personas();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_generate_now',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_generate_now();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_import_now',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_import_now();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_project_details',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_editorial_calendar_page' ) ) {
					$calendar_page = $this->admin_manager->get_editorial_calendar_page();
					if ( $calendar_page ) {
						$calendar_page->ajax_get_project_details();
					}
				}
			}
		);
	}

	// =========================================================================
	// AJAX hooks — Deduplication & clustering
	// =========================================================================

	/**
	 * Register deduplication and clustering AJAX hooks.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_deduplication_hooks() {
		add_action(
			'wp_ajax_wisewand_scan_duplicates',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_scan_duplicates();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_get_clusters',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_get_clusters();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_set_cluster_primary',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_set_cluster_primary();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_ungroup_cluster',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_ungroup_cluster();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_delete_cluster_variants',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_delete_cluster_variants();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_approve_cluster',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_approve_cluster();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_check_cannibalization',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_check_cannibalization();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_run_clustering_scan',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_run_clustering_scan();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_ai_match_categories',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_ai_match_categories();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_count_keywords_for_matching',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_count_keywords_for_matching();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_save_keyword_author_categories',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_save_keyword_author_categories();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_check_ranking_conflicts',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_seo_intelligence_page' ) ) {
					$seo_page = $this->admin_manager->get_seo_intelligence_page();
					if ( $seo_page ) {
						$seo_page->ajax_check_ranking_conflicts();
					}
				}
			}
		);
	}

	// =========================================================================
	// AJAX hooks — RSS feeds
	// =========================================================================

	/**
	 * Register RSS feeds AJAX hooks.
	 *
	 * @since 2.8.2
	 */
	private function init_ajax_rss_feeds_hooks() {
		add_action(
			'wp_ajax_wisewand_rss_test_connection',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_test_rss_connection();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_list_feeds',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_list_feeds();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_sync_feed',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_sync_feed();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_bulk_sync',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_bulk_sync();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_get_articles',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_get_articles();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_import_article',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_import_article();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_bulk_import',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_bulk_import();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_get_feed_settings',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_get_feed_settings();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_save_feed_settings',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_save_feed_settings();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_bulk_activate',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_bulk_activate();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_bulk_deactivate',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_bulk_deactivate();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_get_global_publication_settings',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_get_global_publication_settings();
					}
				}
			}
		);
		add_action(
			'wp_ajax_wisewand_rss_cleanup_data',
			function () {
				if ( $this->admin_manager && method_exists( $this->admin_manager, 'get_rss_page' ) ) {
					$rss_page = $this->admin_manager->get_rss_page();
					if ( $rss_page ) {
						$rss_page->ajax_cleanup_rss_data();
					}
				}
			}
		);
	}

	// =========================================================================
	// CRON hooks
	// =========================================================================

	/**
	 * Register WP-Cron hooks.
	 *
	 * Covers editorial calendar automation, RSS feed sync, pull-mode retry,
	 * and manual CRON trigger endpoints.
	 *
	 * @since 2.8.2
	 */
	private function init_cron_hooks() {
		// WP-Cron for PULL mode.
		if ( $this->content_manager ) {
			add_action( 'wisewand_pull_retry', array( $this->content_manager, 'process_pull_retry' ) );
		}

		// Hook to reschedule RSS sync when frequency option changes.
		if ( $this->cron_scheduler ) {
			add_action( 'update_option_wisewand_rss_default_import_frequency', array( $this->cron_scheduler, 'reschedule_rss_sync' ), 10, 2 );
		}

		// Scheduled events.
		add_action( 'wisewand_editorial_calendar_cron', array( $this->plugin, 'run_calendar_cron' ) );
		add_action( 'wisewand_check_article_status', array( $this->plugin, 'run_status_check_cron' ) );
		add_action( 'wisewand_import_ready_content', array( $this->plugin, 'run_import_ready_content' ) );

		// v2.3.0: Simplified to single RSS sync hook.
		add_action( 'wisewand_rss_sync', array( $this->plugin, 'run_rss_sync' ) );

		// Manual CRON trigger endpoints (dashboard refresh button).
		add_action( 'wp_ajax_wisewand_manual_cron_trigger', array( $this->plugin, 'ajax_manual_cron_trigger' ) );
		add_action( 'wp_ajax_wisewand_manual_full_cron_trigger', array( $this->plugin, 'ajax_manual_full_cron_trigger' ) );
		add_action( 'wp_ajax_wisewand_manual_rss_trigger', array( $this->plugin, 'ajax_manual_rss_trigger' ) );
	}

	// =========================================================================
	// REST API hooks
	// =========================================================================

	/**
	 * Register REST API hooks.
	 *
	 * Registers the REST controller routes and the webhook receiver.
	 *
	 * @since 2.8.2
	 */
	private function init_rest_api_hooks() {
		if ( $this->rest_controller ) {
			add_action( 'rest_api_init', array( $this->rest_controller, 'register_routes' ) );
		}

		// Webhook Receiver — instantiated inside the callback to defer class loading.
		add_action(
			'rest_api_init',
			function () {
				if ( ! class_exists( 'Wisewand\\API\\Webhook_Receiver' ) ) {
					require_once WISEWAND_PLUGIN_DIR . 'includes/API/Webhook_Receiver.php';
				}

				if ( $this->options && $this->logger ) {
					$webhook_receiver = new \Wisewand\API\Webhook_Receiver( $this->options, $this->logger );
					$webhook_receiver->register_routes();
				}
			}
		);
	}

	// =========================================================================
	// Content hooks
	// =========================================================================

	/**
	 * Register content-related hooks.
	 *
	 * Currently handles WordPress publish status sync to the Wisewand API.
	 *
	 * @since 2.8.2
	 */
	private function init_content_hooks() {
		add_action( 'transition_post_status', array( $this->plugin, 'sync_publish_status_to_wisewand' ), 10, 3 );
	}

	// =========================================================================
	// Settings hooks
	// =========================================================================

	/**
	 * Register settings-related hooks.
	 *
	 * Placeholder — future settings filters will be added here.
	 *
	 * @since 2.8.2
	 */
	private function init_settings_hooks() {
		// Future: Add settings filters here.
	}

	// =========================================================================
	// Editor hooks
	// =========================================================================

	/**
	 * Register Widget Editor Support hooks (v2.8.0).
	 *
	 * Covers Alpine.js KSES whitelisting, tag protection, TinyMCE
	 * configuration, Classic Editor forcing, frontend Alpine.js enqueueing,
	 * and standalone plugin conflict detection.
	 *
	 * @since 2.8.2
	 */
	private function init_editor_hooks() {
		if ( ! $this->widget_editor_support ) {
			return;
		}

		// Skip our hooks if the standalone plugin class is already loaded
		// (prevents double-registration during the same request where we deactivate it).
		if ( class_exists( 'Alpine_CSP_Support' ) ) {
			add_action( 'admin_init', array( $this->widget_editor_support, 'deactivate_standalone_plugin' ) );
			add_action( 'admin_notices', array( $this->widget_editor_support, 'show_standalone_deactivation_notice' ) );
			return;
		}

		// KSES: Whitelist Alpine.js directives.
		add_filter( 'wp_kses_allowed_html', array( $this->widget_editor_support, 'allow_alpine_kses' ), 50, 2 );

		// Content save: Convert Alpine.js shorthand (priority 5, before KSES).
		add_filter( 'content_save_pre', array( $this->widget_editor_support, 'convert_shorthand' ), 5 );

		// REST API: Convert shorthand on REST saves.
		add_filter( 'rest_pre_insert_post', array( $this->widget_editor_support, 'convert_rest' ), 10, 2 );

		// Editor: Protect tags before TinyMCE loads (priority 1).
		add_filter( 'the_editor_content', array( $this->widget_editor_support, 'protect_tags_before_editor' ), 1 );

		// Content save: Restore protected tags after KSES (priority 50).
		add_filter( 'content_save_pre', array( $this->widget_editor_support, 'restore_tags_after_kses' ), 50 );

		// TinyMCE: Configure for widget preview.
		add_filter( 'tiny_mce_before_init', array( $this->widget_editor_support, 'configure_tinymce' ) );

		// Block editor: Force Classic Editor for widget posts.
		add_filter( 'use_block_editor_for_post', array( $this->widget_editor_support, 'maybe_force_classic_editor' ), 10, 2 );

		// Admin: Show notice on widget post edit screens.
		add_action( 'edit_form_top', array( $this->widget_editor_support, 'show_wisewand_notice' ) );

		// Frontend: Enqueue Alpine.js CSP build (smart detection).
		add_action( 'wp_enqueue_scripts', array( $this->widget_editor_support, 'enqueue_alpine' ) );

		// Frontend: Prevent cache plugins from deferring Alpine.js.
		add_filter( 'script_loader_tag', array( $this->widget_editor_support, 'exclude_alpine_from_cache' ), 10, 2 );

		// Conflict detection: Deactivate standalone plugin if active.
		add_action( 'admin_init', array( $this->widget_editor_support, 'deactivate_standalone_plugin' ) );
		add_action( 'admin_notices', array( $this->widget_editor_support, 'show_standalone_deactivation_notice' ) );
	}

	// =========================================================================
	// Diagnostic hooks
	// =========================================================================

	/**
	 * Register diagnostic and monitoring hooks.
	 *
	 * Placeholder — dashboard widgets and logging hooks will be added here.
	 *
	 * @since 2.8.2
	 */
	private function init_diagnostic_hooks() {
		// Future: Add dashboard widgets, logging, monitoring hooks here.
	}
}
