<?php
/**
 * OpenRouter search capability integration.
 *
 * @package SmartyPress_AI_Engine
 */

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

/**
 * Adds Sonar/SEO analysis model capabilities.
 */
class SmartyPress_AI_SEO_Analysis_OpenRouter_Capabilities {

	const SONAR_MODEL_PREFIX = 'perplexity/sonar';
	const EXCLUDED_SONAR_MODELS = array(
		'perplexity/sonar-deep-research',
	);

	/**
	 * Add hooks.
	 *
	 * @return void
	 */
	public function register_hooks() {
		add_filter( 'smartypress_ai_openrouter_model_capabilities', array( $this, 'add_openrouter_web_search_capability' ), 10, 3 );
		add_filter( 'smartypress_ai_openrouter_text_fallback_models', array( $this, 'add_openrouter_fallback_models' ), 10, 1 );
		add_filter( 'smartypress_ai_openrouter_web_search_params', array( $this, 'build_openrouter_web_search_params' ), 10, 3 );
	}

	/**
	 * Add web search capability to OpenRouter search-native models.
	 *
	 * @param array  $capabilities Normalized capabilities.
	 * @param string $model_id     Model ID.
	 * @param array  $model        Raw or normalized model metadata.
	 * @return array
	 */
	public function add_openrouter_web_search_capability( $capabilities, $model_id, $model ) {
		$capabilities['web_search'] = $this->is_openrouter_web_search_model( $model_id, $model );

		return $capabilities;
	}

	/**
	 * Add search-native OpenRouter models to fallback list.
	 *
	 * @param array $models Fallback models keyed by model ID.
	 * @return array
	 */
	public function add_openrouter_fallback_models( $models ) {
		$search_models = array(
			'perplexity/sonar'               => 'Sonar',
			'perplexity/sonar-pro'           => 'Sonar Pro',
			'perplexity/sonar-reasoning'     => 'Sonar Reasoning',
			'perplexity/sonar-reasoning-pro' => 'Sonar Reasoning Pro',
		);

		return array_merge( $search_models, is_array( $models ) ? $models : array() );
	}

	/**
	 * Build optional OpenRouter web search request params.
	 *
	 * @param array  $search_params Existing search params.
	 * @param string $model         Model ID.
	 * @param array  $params        Provider params.
	 * @return array
	 */
	public function build_openrouter_web_search_params( $search_params, $model, $params ) {
		if ( ! empty( $params['search_domain_filter'] ) && is_array( $params['search_domain_filter'] ) ) {
			$search_params['search_domain_filter'] = array_values( array_map( 'sanitize_text_field', $params['search_domain_filter'] ) );
		}

		if ( ! empty( $params['search_recency_filter'] ) ) {
			$search_params['search_recency_filter'] = sanitize_text_field( $params['search_recency_filter'] );
		}

		if ( ! empty( $params['web_search_options'] ) && is_array( $params['web_search_options'] ) ) {
			$search_params['web_search_options'] = $params['web_search_options'];
		}

		return $search_params;
	}

	/**
	 * Determine if a model belongs to the Sonar allowlist.
	 *
	 * @param string $model_id Model ID.
	 * @return bool
	 */
	public function is_openrouter_sonar_model( $model_id ) {
		$model_id = strtolower( (string) $model_id );

		if ( in_array( $model_id, self::EXCLUDED_SONAR_MODELS, true ) ) {
			return false;
		}

		return preg_match( '#^' . preg_quote( self::SONAR_MODEL_PREFIX, '#' ) . '(?:-|$)#', $model_id ) === 1;
	}

	/**
	 * Determine if an OpenRouter model is search-native.
	 *
	 * @param string $model_id Model ID.
	 * @param array  $model    Model metadata.
	 * @return bool
	 */
	private function is_openrouter_web_search_model( $model_id, array $model = array() ) {
		return $this->is_openrouter_sonar_model( $model_id );
	}
}
