<?php

namespace WPD_Platform;

use WPD_Platform\Services\RemoteService;
use WPD_Platform\Services\TriggerDispatcher;

/**
 * Class Hooks
 *
 * Handles WordPress action and filter hooks for the platform.
 *
 * @package WPD_Platform
 * @since 1.0.0
 */
class Hooks {

	/**
	 * Initialize hooks and filters.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		// Remote service initialization
		add_action( 'init', [ RemoteService::instance(), 'run' ], 999 );

		// Initialize trigger dispatcher to send integration triggers to CHQ
		add_action( 'init', [ TriggerDispatcher::instance(), 'init' ] );

		// Admin menu and AJAX handlers
		add_action( 'admin_menu', [ Admin::instance(), 'settings_page' ] );
		add_action( 'wp_ajax_dollie_connect_remove_site', [ Admin::instance(), 'ajax_callback_remove_site' ] );
		add_action( 'wp_ajax_dollie_connect_site', [ Admin::instance(), 'ajax_callback_connect_site' ] );

		// Custom endpoint for Application Passwords connection setup (avoids REST API dependency)
		add_action( 'init', [ Admin::instance(), 'handle_setup_connection_request' ], 1 );

		// REST API endpoint for connection setup (preferred over custom endpoint)
		add_action( 'rest_api_init', [ Admin::instance(), 'register_rest_routes' ] );

		// Handle redirect-back callback from CHQ after plugin-active connection
		add_action( 'admin_init', [ Admin::instance(), 'handle_connection_callback' ] );

		// Whitelabel functionality
		add_filter( 'plugin_row_meta', [ Whitelabel::instance(), 'whitelabel' ], 10, 2 );
		add_action( 'wpd_platform/after/register_site', [ Whitelabel::instance(), 'after_register_site' ] );

		// System update hooks
		add_action( 'upgrader_process_complete', [ Admin::instance(), 'trigger_update_hook' ] );
		add_action( 'activated_plugin', [ Admin::instance(), 'trigger_update_hook' ] );
		add_action( 'deactivated_plugin', [ Admin::instance(), 'trigger_update_hook' ] );
		add_action( 'deleted_plugin', [ Admin::instance(), 'trigger_update_hook' ] );
		add_action( 'after_switch_theme', [ Admin::instance(), 'trigger_update_hook' ] );
		add_action( 'deleted_theme', [ Admin::instance(), 'trigger_update_hook' ] );

		// User management hooks
		add_action( 'user_register', [ Admin::instance(), 'trigger_update_hook' ] );
		add_action( 'wp_update_user', [ Admin::instance(), 'trigger_update_hook' ] );
		add_action( 'deleted_user', [ Admin::instance(), 'trigger_update_hook' ] );

		// Plugin auto-update functionality
		add_filter( 'auto_update_plugin', [ Admin::instance(), 'enable_auto_update_plugin' ], 10, 2 );
	}
}
