<?php

namespace App\Domains\AI\Services;

use App\Domains\AI\Contracts\AiProvider;
use App\Domains\AI\Providers\HeuristicAiProvider;
use App\Domains\AI\Providers\OpenAiCompatibleProvider;
use App\Support\PlatformSettings;
use InvalidArgumentException;

class AiProviderManager
{
    public function __construct(
        protected HeuristicAiProvider $heuristicProvider,
        protected OpenAiCompatibleProvider $openAiProvider,
    ) {
    }

    public function resolve(?string $providerKey = null): AiProvider
    {
        $providerKey ??= (string) PlatformSettings::get('AI_PROVIDER', config('ai.default_provider', 'heuristic'));

        return match ($providerKey) {
            'heuristic' => $this->heuristicProvider,
            'openai' => $this->openAiProvider,
            default => throw new InvalidArgumentException("Unsupported AI provider [{$providerKey}]."),
        };
    }
}
