<?php
/**
 * SEO Analysis Module Class
 *
 * Bootstraps SEO analysis capabilities, REST routes, plan hooks, and admin assets.
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * SEO Analysis module.
 */
class SmartyPress_AI_SEO_Analysis_Module extends SmartyPress_AI_Module_Base {

	/**
	 * OpenRouter capability helper.
	 *
	 * @var SmartyPress_AI_SEO_Analysis_OpenRouter_Capabilities
	 */
	private $capabilities;

	/**
	 * Plan hook integration.
	 *
	 * @var SmartyPress_AI_SEO_Analysis_Plan_Hooks
	 */
	private $plan_hooks;

	/**
	 * Competitor REST controller.
	 *
	 * @var SmartyPress_AI_SEO_Analysis_Competitor_Controller
	 */
	private $competitor_controller;

	/**
	 * Constructor.
	 *
	 * @param array $providers AI provider instances.
	 * @param array $settings  Plugin settings.
	 */
	public function __construct( $providers = array(), $settings = array() ) {
		$this->module_id      = 'seo_analysis';
		$this->module_name    = 'SEO Analysis';
		$this->module_version = '1.0.0';

		$this->capabilities = new SmartyPress_AI_SEO_Analysis_OpenRouter_Capabilities();
		$this->plan_hooks   = new SmartyPress_AI_SEO_Analysis_Plan_Hooks();

		$prompts = new SmartyPress_AI_SEO_Analysis_Prompts( $settings );
		$parser  = new SmartyPress_AI_SEO_Analysis_Response_Parser();
		$service = new SmartyPress_AI_SEO_Analysis_Competitor_Research_Service( $prompts, $parser );
		$this->competitor_controller = new SmartyPress_AI_SEO_Analysis_Competitor_Controller(
			$providers,
			$settings,
			$this->capabilities,
			$service
		);

		parent::__construct( $providers, $settings );
	}

	/**
	 * Register module settings.
	 *
	 * @return void
	 */
	public function register_settings() {
		do_action( 'smartypress_ai_module_seo_analysis_settings', $this );
	}

	/**
	 * Get module settings.
	 *
	 * @return array
	 */
	public function get_module_settings() {
		return array(
			'enabled' => $this->is_enabled(),
		);
	}

	/**
	 * Register hooks.
	 *
	 * @return void
	 */
	protected function register_hooks() {
		$this->capabilities->register_hooks();
		$this->plan_hooks->register_hooks();

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ), 30 );
	}

	/**
	 * Register REST routes.
	 *
	 * @return void
	 */
	protected function register_rest_routes() {
		add_action( 'rest_api_init', array( $this, 'register_module_rest_routes' ), 20 );
	}

	/**
	 * Register module REST routes.
	 *
	 * @return void
	 */
	public function register_module_rest_routes() {
		$this->competitor_controller->register_routes();
	}

	/**
	 * Enqueue SEO analysis UI extension for the content generation screen.
	 *
	 * @param string $hook Current admin page hook.
	 * @return void
	 */
	public function enqueue_admin_assets( $hook ) {
		if ( ! $this->is_enabled() || false === strpos( $hook, 'smartypress-ai' ) ) {
			return;
		}

		$dist_dir = SPAI_SA_PLUGIN_DIR . '/assets/dist/';
		$dist_url = SPAI_SA_PLUGIN_URL . '/assets/dist/';
		$js_file = $dist_dir . 'seo-analysis.js';

		if ( ! file_exists( $js_file ) || ! wp_script_is( 'smartypress-ai-content-generator', 'enqueued' ) ) {
			return;
		}

		$asset_file = $dist_dir . 'seo-analysis.asset.php';
		$asset = file_exists( $asset_file )
			? require $asset_file
			: array(
				'dependencies' => array( 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-hooks', 'wp-i18n', 'smartypress-ai-content-generator' ),
				'version'      => SMARTYPRESS_AI_VERSION,
			);

		wp_enqueue_script(
			'smartypress-ai-seo-analysis',
			$dist_url . 'seo-analysis.js',
			$asset['dependencies'],
			filemtime( $js_file ),
			true
		);

		wp_set_script_translations(
			'smartypress-ai-seo-analysis',
			'smartypress-ai-engine',
			SMARTYPRESS_AI_PLUGIN_DIR . 'languages'
		);

		$css_file = $dist_dir . 'seo-analysis.css';
		if ( file_exists( $css_file ) ) {
			wp_enqueue_style(
				'smartypress-ai-seo-analysis',
				$dist_url . 'seo-analysis.css',
				array( 'wp-components', 'smartypress-ai-content-generator' ),
				filemtime( $css_file )
			);
		}

		wp_localize_script(
			'smartypress-ai-seo-analysis',
			'smartypressAISeoAnalysis',
			array(
				'restUrl' => 'smartypress/v1/',
				'nonce'   => wp_create_nonce( 'wp_rest' ),
			)
		);
	}
}
