"""Tests for LangChain provider factory."""
from __future__ import annotations

from unittest.mock import patch

import pytest

from claim_validator.exceptions import ConfigurationError


class TestBuildChatModel:
    def test_groq_provider(self) -> None:
        from claim_validator.chains.provider_factory import build_chat_model

        with patch("langchain_groq.ChatGroq") as mock:
            build_chat_model({"provider": "groq", "model": "llama-3.3-70b-versatile", "api_key": "gsk_test"})
            mock.assert_called_once()
            kwargs = mock.call_args.kwargs
            assert kwargs["model"] == "llama-3.3-70b-versatile"

    def test_openai_provider(self) -> None:
        from claim_validator.chains.provider_factory import build_chat_model

        with patch("langchain_openai.ChatOpenAI") as mock:
            build_chat_model({"provider": "openai", "model": "gpt-4o", "api_key": "sk-test"})
            mock.assert_called_once()

    def test_openai_compatible_provider(self) -> None:
        from claim_validator.chains.provider_factory import build_chat_model

        with patch("langchain_openai.ChatOpenAI") as mock:
            build_chat_model({"provider": "openai_compatible", "model": "llama3", "api_key": "na", "base_url": "http://localhost:11434/v1"})
            kwargs = mock.call_args.kwargs
            assert kwargs["base_url"] == "http://localhost:11434/v1"

    def test_anthropic_provider(self) -> None:
        from claim_validator.chains.provider_factory import build_chat_model

        with patch("langchain_anthropic.ChatAnthropic") as mock:
            build_chat_model({"provider": "anthropic", "model": "claude-sonnet-4-5-20241022", "api_key": "sk-ant-test"})
            mock.assert_called_once()

    def test_unknown_provider_raises(self) -> None:
        from claim_validator.chains.provider_factory import build_chat_model

        with pytest.raises(ConfigurationError, match="Unknown LLM provider"):
            build_chat_model({"provider": "unknown", "model": "x", "api_key": "x"})

    def test_default_temperature_and_max_tokens(self) -> None:
        from claim_validator.chains.provider_factory import build_chat_model

        with patch("langchain_groq.ChatGroq") as mock:
            build_chat_model({"provider": "groq", "model": "test", "api_key": "test"})
            kwargs = mock.call_args.kwargs
            assert kwargs["temperature"] == 0.0
            assert kwargs["max_tokens"] == 4096
