<?php
/**
 * Answyr — AI Provider registry.
 *
 * Holds the set of available providers so settings can enumerate them and the
 * AI client can resolve the active one by id.
 *
 * @package Answyr
 */

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

final class Answyr_Provider_Registry {

	/** @var array<string,Answyr_AI_Provider> */
	private static array $providers = array();

	public static function register( Answyr_AI_Provider $provider ): void {
		self::$providers[ $provider->id() ] = $provider;
	}

	public static function get( string $id ): ?Answyr_AI_Provider {
		return self::$providers[ $id ] ?? null;
	}

	/** @return array<string,Answyr_AI_Provider> */
	public static function all(): array {
		return self::$providers;
	}

	/**
	 * id => label, for a settings <select>.
	 *
	 * @return array<string,string>
	 */
	public static function choices(): array {
		$out = array();
		foreach ( self::$providers as $id => $provider ) {
			$out[ $id ] = $provider->label();
		}
		return $out;
	}

	/**
	 * provider id => ( model id => label ), for cascading model selects.
	 *
	 * @return array<string,array<string,string>>
	 */
	public static function model_choices(): array {
		$out = array();
		foreach ( self::$providers as $id => $provider ) {
			$out[ $id ] = $provider->models();
		}
		return $out;
	}
}
