from __future__ import annotations

from typing import Any

from langchain_openai import ChatOpenAI

from cleveragents.providers.llm.langchain_chat_provider import LangChainChatProvider


class OpenAIChatProvider(LangChainChatProvider):
    """Concrete LangChain-backed provider for OpenAI chat models."""

    def __init__(
        self,
        *,
        api_key: str,
        model: str = "gpt-4o",
        max_retries: int = 3,
        organization: str | None = None,
        **llm_kwargs: Any,
    ) -> None:
        if not api_key:
            raise ValueError("OpenAI API key is required")

        def factory(resolved_model: str) -> ChatOpenAI:
            kwargs: dict[str, Any] = {"api_key": api_key, "model": resolved_model}
            if organization:
                kwargs["organization"] = organization
            if llm_kwargs:
                kwargs.update(llm_kwargs)
            return ChatOpenAI(**kwargs)

        super().__init__(
            name="openai",
            model_id=model,
            llm_factory=factory,
            max_retries=max_retries,
            supports_streaming=True,
        )
