<?php
/**
 * Main Plugin Class
 *
 * @package Specflux_Marketing_Analytics
 */

namespace Specflux_Marketing_Analytics;

use Specflux_Marketing_Analytics\Utils\Permission_Manager;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
 * Main plugin class that initializes all components
 */
class Plugin {

	/**
	 * The loader that's responsible for maintaining and registering all hooks.
	 *
	 * @var Loader
	 */
	protected $loader;

	/**
	 * Initialize the plugin
	 */
	public function __construct() {
		$this->load_dependencies();
		$this->define_admin_hooks();
		$this->define_ajax_hooks();
		$this->define_abilities_hooks();
		$this->define_review_prompt_hooks();
	}

	/**
	 * Load required dependencies
	 */
	private function load_dependencies() {
		$this->loader = new Loader();
	}

	/**
	 * Register admin-related hooks
	 */
	private function define_admin_hooks() {
		if ( ! is_admin() ) {
			return;
		}

		$this->loader->add_action( 'admin_init', Permission_Manager::class, 'register_capabilities' );

		$admin = new Admin\Admin();

		$this->loader->add_action( 'admin_menu', $admin, 'add_admin_menu' );
		$this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_scripts' );
		$this->loader->add_action( 'wp_dashboard_setup', $admin, 'register_dashboard_widget' );
		$this->loader->add_action( 'admin_bar_menu', $admin, 'add_admin_bar_item', 100 );
		$this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_admin_bar_styles' );
	}

	/**
	 * Register the WordPress.org review prompt
	 *
	 * Registered outside the `is_admin()` guard because the fetch counter has
	 * to observe `specflux_mac_data_fetched` wherever it fires (REST/MCP
	 * requests included). The notice itself only renders on `admin_notices`,
	 * and only on this plugin's own screens.
	 */
	private function define_review_prompt_hooks() {
		$review_prompt = new Admin\Review_Prompt();
		$review_prompt->init();
	}

	/**
	 * Register AJAX hooks
	 */
	private function define_ajax_hooks() {
		$ajax_handler = new Admin\Ajax_Handler();
		$ajax_handler->register_hooks();

		// Register chat AJAX handlers.
		$chat_ajax = new Chat\Chat_Ajax_Handler();
		$chat_ajax->register_handlers();
	}

	/**
	 * Register abilities-related hooks
	 */
	private function define_abilities_hooks() {
		$abilities_registrar = new Abilities\Abilities_Registrar();

		// Register category when categories are initialized.
		$this->loader->add_action( 'wp_abilities_api_categories_init', $abilities_registrar, 'register_category' );

		// Register abilities when the Abilities API is ready.
		$this->loader->add_action( 'wp_abilities_api_init', $abilities_registrar, 'register_all_abilities' );
	}

	/**
	 * Run the plugin
	 */
	public function run() {
		$this->loader->run();

		/**
		 * Fires after the free plugin is fully loaded.
		 *
		 * Pro add-on and third-party plugins can hook here for initialization.
		 *
		 * @param Plugin $this The main plugin instance.
		 */
		do_action( 'specflux_mac_loaded', $this );
	}
}
