<?php

namespace XZD\AI\V5\AI;

use XZD\AI\V5\AI\Providers\DeepSeekProvider;
use XZD\AI\V5\AI\Providers\OpenAIProvider;
use XZD\AI\V5\AI\Providers\QwenProvider;
use XZD\AI\V5\AI\Providers\ZhipuProvider;
use XZD\AI\V5\AI\Providers\KimiProvider;
use XZD\AI\V5\AI\Providers\DoubaoProvider;
use XZD\AI\V5\AI\Providers\SiliconFlowProvider;
use XZD\AI\V5\AI\Providers\MiMoProvider;
use XZD\AI\V5\AI\Providers\CustomProvider;

/**
 * Provider 工厂 — 从连接配置创建对应的 Provider 实例
 *
 * @example ProviderFactory::create(['provider'=>'deepseek','api_key'=>'sk-...','model'=>'deepseek-v4-pro'])
 */
class ProviderFactory
{
    private const PROVIDER_MAP = [
        'deepseek'    => DeepSeekProvider::class,
        'openai'      => OpenAIProvider::class,
        'claude'      => ClaudeProvider::class,
        'google'      => GeminiProvider::class,
        'qwen'        => QwenProvider::class,
        'zhipu'       => ZhipuProvider::class,
        'kimi'        => KimiProvider::class,
        'doubao'      => DoubaoProvider::class,
        'siliconflow' => SiliconFlowProvider::class,
        'mimo'        => MiMoProvider::class,
        'custom'      => CustomProvider::class,
    ];

    /**
     * 从连接配置创建 Provider
     */
    public static function create(array $connection): ProviderInterface
    {
        $provider = $connection['provider'] ?? 'custom';
        $class = self::PROVIDER_MAP[$provider] ?? CustomProvider::class;

        return new $class($connection);
    }

    /**
     * 获取厂商标识列表
     */
    public static function getProviderKeys(): array
    {
        return array_keys(self::PROVIDER_MAP);
    }

    /**
     * 检查厂商是否支持
     */
    public static function isSupported(string $provider): bool
    {
        return isset(self::PROVIDER_MAP[$provider]);
    }
}
