"""Provider 레지스트리 및 팩토리."""

from __future__ import annotations

import importlib
from typing import TYPE_CHECKING

if TYPE_CHECKING:
	from dartlab.engines.llmAnalyzer.providers.base import BaseProvider
	from dartlab.engines.llmAnalyzer.types import LLMConfig

_PROVIDER_MAP: dict[str, str] = {
	"openai": "dartlab.engines.llmAnalyzer.providers.openai_compat.OpenAICompatProvider",
	"claude": "dartlab.engines.llmAnalyzer.providers.claude.ClaudeProvider",
	"ollama": "dartlab.engines.llmAnalyzer.providers.ollama.OllamaProvider",
	"custom": "dartlab.engines.llmAnalyzer.providers.openai_compat.OpenAICompatProvider",
	"claude-code": "dartlab.engines.llmAnalyzer.providers.claude_code.ClaudeCodeProvider",
	"codex": "dartlab.engines.llmAnalyzer.providers.codex.CodexProvider",
}


def create_provider(config: "LLMConfig") -> "BaseProvider":
	"""LLMConfig로부터 적절한 provider 인스턴스 생성."""
	class_path = _PROVIDER_MAP.get(config.provider)
	if class_path is None:
		raise ValueError(
			f"지원하지 않는 provider: '{config.provider}'. "
			f"지원: {list(_PROVIDER_MAP.keys())}"
		)
	module_path, class_name = class_path.rsplit(".", 1)
	mod = importlib.import_module(module_path)
	cls = getattr(mod, class_name)
	return cls(config)


def register_provider(name: str, class_path: str) -> None:
	"""새 provider 등록 (확장용).

	Example::

		register_provider("gemini", "mypackage.gemini.GeminiProvider")
	"""
	_PROVIDER_MAP[name] = class_path


def available_providers() -> list[str]:
	"""등록된 provider 이름 목록."""
	return list(_PROVIDER_MAP.keys())
