<?php
/**
 * AI Providers Factory Class
 *
 * Manages AI provider instances
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * AI Providers factory class
 */
class SmartyPress_AI_Providers {

	/**
	 * Provider instances
	 *
	 * @var array
	 */
	private static $instances = array();

	/**
	 * Settings
	 *
	 * @var array
	 */
	private static $settings = array();

	/**
	 * Initialize providers
	 *
	 * @param array $settings Plugin settings.
	 * @return array Provider instances.
	 */
	public static function init( $settings = array() ) {
		self::$settings = $settings;
		self::$instances = array();

		// Initialize OpenAI provider
		if ( ! empty( $settings['openai_api_key'] ) ) {
			$openai_provider = new SmartyPress_AI_OpenAI_Provider(
				$settings['openai_api_key'],
				$settings['openai_model'] ?? 'gpt-4o'
			);

			// Configure proxy if enabled
			if ( ! empty( $settings['openai_use_proxy'] ) && ! empty( $settings['openai_proxy_url'] ) ) {
				$openai_provider->set_proxy( $settings['openai_proxy_url'] );
			}

			self::$instances['openai'] = $openai_provider;
		}

		// Initialize Deepseek provider
		if ( ! empty( $settings['deepseek_api_key'] ) ) {
			self::$instances['deepseek'] = new SmartyPress_AI_Deepseek_Provider(
				$settings['deepseek_api_key'],
				$settings['deepseek_model'] ?? 'deepseek-v4-flash'
			);
		}

		// Initialize Anthropic Claude provider
		if ( ! empty( $settings['anthropic_api_key'] ) ) {
			self::$instances['anthropic'] = new SmartyPress_AI_Anthropic_Provider(
				$settings['anthropic_api_key'],
				$settings['anthropic_model'] ?? 'claude-sonnet-4-5'
			);
		}

		// Initialize OpenRouter provider
		if ( ! empty( $settings['openrouter_api_key'] ) ) {
			self::$instances['openrouter'] = new SmartyPress_AI_OpenRouter_Provider(
				$settings['openrouter_api_key'],
				$settings['openrouter_model'] ?? 'openai/gpt-4o-mini',
				$settings['openrouter_image_model'] ?? 'google/gemini-2.5-flash-image',
				isset( $settings['openrouter_enabled_text_models'] ) && is_array( $settings['openrouter_enabled_text_models'] ) ? $settings['openrouter_enabled_text_models'] : array(),
				isset( $settings['openrouter_enabled_image_models'] ) && is_array( $settings['openrouter_enabled_image_models'] ) ? $settings['openrouter_enabled_image_models'] : array(),
				! empty( $settings['openrouter_limit_text_models'] ),
				! empty( $settings['openrouter_limit_image_models'] )
			);
		}

		// Initialize Yandex provider (only for ru_RU locale)
		if ( get_locale() === 'ru_RU' && ! empty( $settings['yandex_api_key'] ) && ! empty( $settings['yandex_folder_id'] ) ) {
			self::$instances['yandex'] = new SmartyPress_AI_Yandex_Provider(
				$settings['yandex_api_key'],
				$settings['yandex_folder_id'],
				$settings['yandex_model'] ?? 'yandexgpt-lite'
			);
		}

		// Initialize GigaChat provider (only for ru_RU locale)
		if ( get_locale() === 'ru_RU' && ! empty( $settings['gigachat_auth_key'] ) ) {
			self::$instances['gigachat'] = new SmartyPress_AI_GigaChat_Provider(
				$settings['gigachat_auth_key'],
				isset( $settings['gigachat_scope'] ) ? $settings['gigachat_scope'] : 'GIGACHAT_API_PERS',
				isset( $settings['gigachat_model'] ) ? $settings['gigachat_model'] : 'GigaChat-2',
				isset( $settings['gigachat_verify_ssl'] ) ? (bool) $settings['gigachat_verify_ssl'] : false,
				isset( $settings['gigachat_base_url'] ) ? $settings['gigachat_base_url'] : SmartyPress_AI_GigaChat_Client::BASE_URI
			);
		}

		return self::$instances;
	}

	/**
	 * Get provider instance
	 *
	 * @param string $provider_name Provider name.
	 * @return SmartyPress_AI_AI_Provider|null Provider instance or null.
	 */
	public static function get_provider( $provider_name ) {
		return self::$instances[ $provider_name ] ?? null;
	}

	/**
	 * Get all available providers
	 *
	 * @return array Available provider instances.
	 */
	public static function get_available_providers() {
		return self::$instances;
	}

	/**
	 * Get available provider names
	 *
	 * @return array Provider names.
	 */
	public static function get_available_provider_names() {
		return array_keys( self::$instances );
	}

	/**
	 * Validate provider name
	 *
	 * @param string $provider_name Provider name.
	 * @return bool True if valid, false otherwise.
	 */
	public static function validate_provider( $provider_name ) {
		return isset( self::$instances[ $provider_name ] );
	}

	/**
	 * Get all provider instances
	 *
	 * @return array All provider instances.
	 */
	public static function get_all() {
		return self::$instances;
	}
}

