<?php
/**
 * Central registry of AI providers for site-wide connections.
 *
 * @package WP_PowerSuite\AI
 */

namespace WP_PowerSuite\AI;

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

/**
 * Defines provider metadata used by AI Connections and consuming modules.
 */
final class ProviderRegistry {

	/**
	 * @return array<string, array{
	 *     label: string,
	 *     key_field: string,
	 *     config_constant: string,
	 *     placeholder: string,
	 *     module_supported: bool,
	 *     api_family: string,
	 *     base_url?: string,
	 *     description?: string
	 * }>
	 */
	public static function definitions(): array {
		/**
		 * Filter AI provider definitions used across PowerSuite.
		 *
		 * @param array<string, array<string, mixed>> $definitions Provider definitions keyed by ID.
		 */
		return apply_filters( 'wpps_ai_provider_definitions', self::baseDefinitions() );
	}

	/** @return list<string> */
	public static function ids(): array {
		return array_keys( self::definitions() );
	}

	/** @return array<string, string> */
	public static function labels(): array {
		$labels = [];
		foreach ( self::definitions() as $id => $definition ) {
			$labels[ $id ] = (string) ( $definition['label'] ?? $id );
		}

		return $labels;
	}

	/** @return list<string> */
	public static function keyFields(): array {
		$fields = [];
		foreach ( self::definitions() as $definition ) {
			$field = (string) ( $definition['key_field'] ?? '' );
			if ( $field !== '' ) {
				$fields[] = $field;
			}
		}

		return array_values( array_unique( $fields ) );
	}

	/**
	 * @return array<string, array{
	 *     field: string,
	 *     input_id: string,
	 *     network_input_id: string,
	 *     config_constant: string,
	 *     placeholder: string,
	 *     description?: string
	 * }>
	 */
	public static function fieldMeta(): array {
		$meta = [];
		foreach ( self::definitions() as $id => $definition ) {
			$meta[ $id ] = [
				'field'            => (string) ( $definition['key_field'] ?? '' ),
				'input_id'         => 'wpps-ai-connections-' . sanitize_key( $id ) . '-key',
				'network_input_id' => 'wpps-ai-connections-network-' . sanitize_key( $id ) . '-key',
				'config_constant'  => (string) ( $definition['config_constant'] ?? '' ),
				'placeholder'      => (string) ( $definition['placeholder'] ?? '' ),
				'description'      => isset( $definition['description'] ) ? (string) $definition['description'] : '',
			];
		}

		return $meta;
	}

	public static function sanitize( string $provider ): string {
		$provider = sanitize_key( $provider );

		return isset( self::definitions()[ $provider ] ) ? $provider : 'openai';
	}

	public static function supportsModules( string $provider ): bool {
		$definition = self::definitions()[ self::sanitize( $provider ) ] ?? null;

		return is_array( $definition ) && ! empty( $definition['module_supported'] );
	}

	public static function apiFamily( string $provider ): string {
		$definition = self::definitions()[ self::sanitize( $provider ) ] ?? null;

		return is_array( $definition ) ? (string) ( $definition['api_family'] ?? 'openai' ) : 'openai';
	}

	public static function baseUrl( string $provider ): string {
		$definition = self::definitions()[ self::sanitize( $provider ) ] ?? null;
		$base_url   = is_array( $definition ) ? trim( (string) ( $definition['base_url'] ?? '' ) ) : '';

		return $base_url !== '' ? rtrim( $base_url, '/' ) : 'https://api.openai.com/v1';
	}

	public static function configConstant( string $provider ): string {
		$definition = self::definitions()[ self::sanitize( $provider ) ] ?? null;

		return is_array( $definition ) ? (string) ( $definition['config_constant'] ?? '' ) : '';
	}

	public static function keyField( string $provider ): string {
		$definition = self::definitions()[ self::sanitize( $provider ) ] ?? null;
		$field      = is_array( $definition ) ? (string) ( $definition['key_field'] ?? '' ) : '';

		return $field !== '' ? $field : 'api_key';
	}

	/**
	 * @return array<string, array{
	 *     label: string,
	 *     key_field: string,
	 *     config_constant: string,
	 *     placeholder: string,
	 *     module_supported: bool,
	 *     api_family: string,
	 *     base_url?: string,
	 *     description?: string
	 * }>
	 */
	private static function baseDefinitions(): array {
		return [
			'openai' => [
				'label'            => __( 'OpenAI', 'wp-powersuite' ),
				'key_field'        => 'api_key',
				'config_constant'  => 'WPPS_AI_OPENAI_KEY',
				'placeholder'      => __( 'Paste your OpenAI API key', 'wp-powersuite' ),
				'module_supported' => true,
				'api_family'       => 'openai',
			],
			'gemini' => [
				'label'            => __( 'Google Gemini', 'wp-powersuite' ),
				'key_field'        => 'gemini_api_key',
				'config_constant'  => 'WPPS_AI_GEMINI_KEY',
				'placeholder'      => __( 'Paste your Gemini API key', 'wp-powersuite' ),
				'module_supported' => true,
				'api_family'       => 'gemini',
			],
			'claude' => [
				'label'            => __( 'Anthropic Claude', 'wp-powersuite' ),
				'key_field'        => 'claude_api_key',
				'config_constant'  => 'WPPS_AI_CLAUDE_KEY',
				'placeholder'      => __( 'Paste your Claude API key', 'wp-powersuite' ),
				'module_supported' => true,
				'api_family'       => 'claude',
			],
			'openrouter' => [
				'label'            => __( 'OpenRouter', 'wp-powersuite' ),
				'key_field'        => 'openrouter_api_key',
				'config_constant'  => 'WPPS_AI_OPENROUTER_KEY',
				'placeholder'      => __( 'Paste your OpenRouter API key', 'wp-powersuite' ),
				'module_supported' => true,
				'api_family'       => 'openai_compatible',
				'base_url'         => 'https://openrouter.ai/api/v1',
			],
			'deepseek' => [
				'label'            => __( 'DeepSeek', 'wp-powersuite' ),
				'key_field'        => 'deepseek_api_key',
				'config_constant'  => 'WPPS_AI_DEEPSEEK_KEY',
				'placeholder'      => __( 'Paste your DeepSeek API key', 'wp-powersuite' ),
				'module_supported' => true,
				'api_family'       => 'openai_compatible',
				'base_url'         => 'https://api.deepseek.com/v1',
			],
			'grok' => [
				'label'            => __( 'Grok', 'wp-powersuite' ),
				'key_field'        => 'grok_api_key',
				'config_constant'  => 'WPPS_AI_GROK_KEY',
				'placeholder'      => __( 'Paste your Grok API key', 'wp-powersuite' ),
				'module_supported' => true,
				'api_family'       => 'openai_compatible',
				'base_url'         => 'https://api.x.ai/v1',
			],
		];
	}
}
