<?php
/**
 * Text model registry for Basic Forms AI generation.
 *
 * @package BasicForms
 */

namespace BasicForms;

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

final class FormAiModels {

	/**
	 * Retired model IDs mapped to the closest current replacement.
	 *
	 * @var array<string, string>
	 */
	private const RETIRED_MAP = [
		'gemini-2.0-flash'           => 'gemini-2.5-flash',
		'gemini-2.0-flash-lite'      => 'gemini-2.5-flash-lite',
		'gemini-1.5-pro'             => 'gemini-2.5-pro',
		'gemini-1.5-flash'           => 'gemini-2.5-flash',
		'claude-3-5-haiku-latest'    => 'claude-haiku-4-5-20251001',
		'claude-3-5-sonnet-latest'   => 'claude-sonnet-4-6',
		'claude-3-haiku-20240307'    => 'claude-haiku-4-5-20251001',
		'grok-2-latest'              => 'grok-4.3',
		'anthropic/claude-3.5-haiku' => 'anthropic/claude-haiku-4.5',
		'google/gemini-2.0-flash-001' => 'google/gemini-2.5-flash',
	];

	/**
	 * @return array<string, array{label: string, models: array<string, string>}>
	 */
	public static function getGroups( string $provider ): array {
		$provider = AiSettings::sanitizeProvider( $provider );

		$groups = match ( $provider ) {
			'gemini'     => self::geminiGroups(),
			'claude'     => self::claudeGroups(),
			'openrouter' => self::openRouterGroups(),
			'deepseek'   => self::deepseekGroups(),
			'grok'       => self::grokGroups(),
			default      => self::openAiGroups(),
		};

		/**
		 * Filter grouped text models for Basic Forms AI generation.
		 *
		 * @param array<string, array{label: string, models: array<string, string>}> $groups
		 * @param string                                                            $provider
		 */
		return apply_filters( 'wpps_basic_forms_ai_model_groups', $groups, $provider );
	}

	/**
	 * @return array<string, string>
	 */
	public static function getList( string $provider ): array {
		$list = [];
		foreach ( self::getGroups( $provider ) as $group ) {
			if ( ! isset( $group['models'] ) || ! is_array( $group['models'] ) ) {
				continue;
			}
			foreach ( $group['models'] as $id => $label ) {
				$list[ (string) $id ] = (string) $label;
			}
		}

		return $list;
	}

	public static function getDefault( string $provider ): string {
		return match ( AiSettings::sanitizeProvider( $provider ) ) {
			'gemini'     => 'gemini-2.5-flash',
			'claude'     => 'claude-haiku-4-5-20251001',
			'openrouter' => 'openai/gpt-4o-mini',
			'deepseek'   => 'deepseek-chat',
			'grok'       => 'grok-4.3',
			default      => 'gpt-4o-mini',
		};
	}

	public static function sanitize( string $model, string $provider ): string {
		$provider = AiSettings::sanitizeProvider( $provider );
		$model    = trim( sanitize_text_field( $model ) );

		if ( $model === '' ) {
			return self::getDefault( $provider );
		}

		if ( isset( self::RETIRED_MAP[ $model ] ) ) {
			$model = self::RETIRED_MAP[ $model ];
		}

		$allowed = array_keys( self::getList( $provider ) );
		if ( in_array( $model, $allowed, true ) ) {
			return $model;
		}

		return self::getDefault( $provider );
	}

	public static function getSettingsHint( string $provider ): string {
		return match ( AiSettings::sanitizeProvider( $provider ) ) {
			'gemini'     => __( 'Gemini 2.5 Flash is the recommended default for fast form generation.', 'wp-powersuite' ),
			'claude'     => __( 'Claude Haiku 4.5 is fast and cost-effective. Use Sonnet 4.6 for complex multi-section forms.', 'wp-powersuite' ),
			'openrouter' => __( 'OpenRouter routes to many models. GPT-4o mini is a solid default for form generation.', 'wp-powersuite' ),
			'deepseek'   => __( 'DeepSeek Chat is the recommended default for form generation.', 'wp-powersuite' ),
			'grok'       => __( 'Grok 4.3 is the recommended default for Grok.', 'wp-powersuite' ),
			default      => __( 'GPT-4o mini is the recommended default. Use GPT-5.5 or GPT-4.1 for large or complex forms.', 'wp-powersuite' ),
		};
	}

	/**
	 * @return array<string, array{label: string, models: array<string, string>}>
	 */
	private static function openAiGroups(): array {
		return [
			'recommended' => [
				'label'  => __( 'Recommended', 'wp-powersuite' ),
				'models' => [
					'gpt-4o-mini'   => __( 'GPT-4o mini — recommended default', 'wp-powersuite' ),
					'gpt-5.4-mini'  => __( 'GPT-5.4 mini — fast and capable', 'wp-powersuite' ),
					'gpt-5.5-mini'  => __( 'GPT-5.5 mini — strong quality at lower cost', 'wp-powersuite' ),
					'gpt-4o'        => __( 'GPT-4o — complex forms', 'wp-powersuite' ),
					'gpt-4.1-mini'  => __( 'GPT-4.1 mini — fast', 'wp-powersuite' ),
					'gpt-4.1'       => __( 'GPT-4.1 — high quality', 'wp-powersuite' ),
					'gpt-5.4'       => __( 'GPT-5.4 — advanced reasoning', 'wp-powersuite' ),
					'gpt-5.5'       => __( 'GPT-5.5 — highest quality', 'wp-powersuite' ),
				],
			],
		];
	}

	/**
	 * @return array<string, array{label: string, models: array<string, string>}>
	 */
	private static function geminiGroups(): array {
		return [
			'recommended' => [
				'label'  => __( 'Recommended', 'wp-powersuite' ),
				'models' => [
					'gemini-2.5-flash'      => __( 'Gemini 2.5 Flash — recommended default', 'wp-powersuite' ),
					'gemini-2.5-flash-lite' => __( 'Gemini 2.5 Flash Lite — lowest cost', 'wp-powersuite' ),
					'gemini-2.5-pro'        => __( 'Gemini 2.5 Pro — complex forms', 'wp-powersuite' ),
					'gemini-3.5-flash'      => __( 'Gemini 3.5 Flash — latest generation', 'wp-powersuite' ),
				],
			],
		];
	}

	/**
	 * @return array<string, array{label: string, models: array<string, string>}>
	 */
	private static function claudeGroups(): array {
		return [
			'recommended' => [
				'label'  => __( 'Recommended', 'wp-powersuite' ),
				'models' => [
					'claude-haiku-4-5-20251001' => __( 'Claude Haiku 4.5 — recommended default', 'wp-powersuite' ),
					'claude-sonnet-4-6'         => __( 'Claude Sonnet 4.6 — complex forms', 'wp-powersuite' ),
					'claude-opus-4-8'           => __( 'Claude Opus 4.8 — highest quality', 'wp-powersuite' ),
				],
			],
		];
	}

	private static function openRouterGroups(): array {
		return [
			'recommended' => [
				'label'  => __( 'Recommended', 'wp-powersuite' ),
				'models' => [
					'openai/gpt-4o-mini'          => __( 'GPT-4o mini — recommended default', 'wp-powersuite' ),
					'openai/gpt-5-mini'           => __( 'GPT-5 mini — fast and capable', 'wp-powersuite' ),
					'openai/gpt-4o'               => __( 'GPT-4o — complex forms', 'wp-powersuite' ),
					'anthropic/claude-haiku-4.5'  => __( 'Claude Haiku 4.5 — fast', 'wp-powersuite' ),
					'google/gemini-2.5-flash'     => __( 'Gemini 2.5 Flash — fast', 'wp-powersuite' ),
				],
			],
		];
	}

	private static function deepseekGroups(): array {
		return [
			'recommended' => [
				'label'  => __( 'Recommended', 'wp-powersuite' ),
				'models' => [
					'deepseek-chat'     => __( 'DeepSeek Chat — recommended default', 'wp-powersuite' ),
					'deepseek-reasoner' => __( 'DeepSeek Reasoner — complex forms', 'wp-powersuite' ),
				],
			],
		];
	}

	private static function grokGroups(): array {
		return [
			'recommended' => [
				'label'  => __( 'Recommended', 'wp-powersuite' ),
				'models' => [
					'grok-4.3' => __( 'Grok 4.3 — recommended default', 'wp-powersuite' ),
				],
			],
		];
	}
}
