<?php
/**
 * Plugin Loader Class
 *
 * Manages plugin initialization and coordinates all components
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main plugin loader class
 */
class SmartyPress_AI_Plugin_Loader {

	/**
	 * Plugin settings
	 *
	 * @var array
	 */
	private $settings;

	/**
	 * AI Providers instances
	 *
	 * @var array
	 */
	private $providers = array();

	/**
	 * Settings instance
	 *
	 * @var SmartyPress_AI_Settings
	 */
	private $settings_manager;

	/**
	 * Module Manager instance
	 *
	 * @var SmartyPress_AI_Module_Manager
	 */
	private $module_manager;

	/**
	 * Available modules configuration
	 *
	 * @var array
	 */
	private $available_modules = array();

	/**
	 * Constructor
	 *
	 * @param array $available_modules Available modules configuration.
	 */
	public function __construct( $available_modules = array() ) {
		$this->settings          = get_option( 'smartypress_ai_settings', array() );
		$this->available_modules = $available_modules;
		$this->settings_manager  = new SmartyPress_AI_Settings();
	}

	/**
	 * Run the plugin
	 */
	public function run() {
		// Initialize AI providers (always loaded)
		$this->init_providers();

		// Initialize WordPress Connectors bridge when available.
		$this->init_wp_connectors_bridge();

		// Initialize Module Manager
		$this->module_manager = new SmartyPress_AI_Module_Manager( $this->settings_manager, $this->available_modules );

		// Load enabled modules
		$this->module_manager->load_modules();

		// Initialize REST API (always, even outside admin)
		$this->init_rest_api();

		// Initialize admin components (only in admin)
		if ( is_admin() ) {
			$this->init_admin();
		}
	}

	/**
	 * Initialize AI providers
	 */
	private function init_providers() {
		$this->providers = SmartyPress_AI_Providers::init( $this->settings );
	}

	/**
	 * Initialize WordPress Connectors integration.
	 */
	private function init_wp_connectors_bridge() {
		if ( class_exists( 'SmartyPress_AI_WP_Connectors_Bridge' ) ) {
			new SmartyPress_AI_WP_Connectors_Bridge();
		}
	}

	/**
	 * Get modules to load (filtered by settings)
	 *
	 * @return array Modules to load.
	 */
	public function get_load_plugin_modules() {
		if ( $this->module_manager ) {
			return $this->module_manager->get_load_plugin_modules();
		}
		return array();
	}

	/**
	 * Initialize REST API
	 */
	private function init_rest_api() {
		// Initialize components page (REST API)
		$components_page = new SmartyPress_AI_Components_Page();

		// Initialize settings page (REST API)
		$settings_page = new SmartyPress_AI_Settings_Page();

		// Initialize dashboard API (REST API)
		$dashboard_api = new SmartyPress_AI_Dashboard_API();

		// Initialize token usage API (REST API)
		$token_usage_api = new SmartyPress_AI_Token_Usage_API();
	}

	/**
	 * Initialize admin components
	 */
	private function init_admin() {
		// Initialize admin app (main React SPA)
		$admin_app = new SmartyPress_AI_Admin_App();

		// Initialize admin menu
		// Admin menu is now handled by Admin_App class
		// Removed Admin_Menu class to avoid duplicate menu items
	}

	/**
	 * Check REST API permissions
	 *
	 * @return bool
	 */
	public function check_permissions() {
		return current_user_can( 'edit_posts' );
	}

	/**
	 * Check bulk generation permissions
	 *
	 * @return bool
	 */
	public function check_bulk_permissions() {
		return current_user_can( 'publish_posts' );
	}

	/**
	 * Get provider instance
	 *
	 * @param string $provider_name Provider name.
	 * @return SmartyPress_AI_AI_Provider|null
	 */
	public function get_provider( $provider_name ) {
		return $this->providers[ $provider_name ] ?? null;
	}

	/**
	 * Get all providers
	 *
	 * @return array All provider instances.
	 */
	public function get_providers() {
		return $this->providers;
	}

	/**
	 * Get settings manager
	 *
	 * @return SmartyPress_AI_Settings
	 */
	public function get_settings_manager() {
		return $this->settings_manager;
	}

	/**
	 * Get module manager
	 *
	 * @return SmartyPress_AI_Module_Manager
	 */
	public function get_module_manager() {
		return $this->module_manager;
	}
}

