<?php

namespace GriffinForms\Includes\Ai\Transports;

use GriffinForms\Includes\Ai\Contracts\AiClientInterface;

class AiTransportResolver
{
    /**
     * Resolves the appropriate AI client transport based on the desired provider map.
     */
    public function resolve(string $provider, array $config = []): AiClientInterface
    {
        $timeout = isset($config['timeout']) ? (int) $config['timeout'] : 20;

        switch (strtolower($provider)) {
            case 'openai':
                return new RestTransport(
                    $config['api_key'] ?? '',
                    'https://api.openai.com/v1/chat/completions',
                    $config['model'] ?? 'gpt-4o-mini',
                    $timeout,
                    isset($config['max_tokens']) ? (int) $config['max_tokens'] : null
                );
            case 'anthropic':
                return new AnthropicRestTransport(
                    $config['api_key'] ?? '',
                    $config['model'] ?? 'claude-haiku-4-5',
                    $timeout,
                    isset($config['max_tokens']) ? (int) $config['max_tokens'] : null
                );
            case 'gemini':
                return new GeminiRestTransport(
                    $config['api_key'] ?? '',
                    $config['model'] ?? 'gemini-2.0-flash',
                    $timeout,
                    isset($config['max_tokens']) ? (int) $config['max_tokens'] : null
                );
            case 'xai':
                return new RestTransport(
                    $config['api_key'] ?? '',
                    'https://api.x.ai/v1/chat/completions',
                    $config['model'] ?? 'grok-3-mini',
                    $timeout,
                    isset($config['max_tokens']) ? (int) $config['max_tokens'] : null
                );
            case 'mistral':
                return new RestTransport(
                    $config['api_key'] ?? '',
                    'https://api.mistral.ai/v1/chat/completions',
                    $config['model'] ?? 'mistral-small-latest',
                    $timeout,
                    isset($config['max_tokens']) ? (int) $config['max_tokens'] : null
                );
            default:
                return new RestTransport(
                    $config['api_key'] ?? '',
                    $config['endpoint'] ?? '',
                    $config['model'] ?? '',
                    $timeout,
                    isset($config['max_tokens']) ? (int) $config['max_tokens'] : null
                );
        }
    }
}
