# #region Test.PreviewResolveProvider [C:3] [TYPE Module] [SEMANTICS test, translate, preview, provider, model, resolve]
# @BRIEF Tests for preview_resolve_provider.py — resolve_provider_model.
# @RELATION BINDS_TO -> [resolve_provider_model]
# @TEST_CONTRACT: resolve_provider_model -> str|None | returns default model or None

import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "src"))

import os
os.environ.setdefault("SECRET_KEY", "test-secret-key-for-testing")
os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:")
os.environ.setdefault("AUTH_DATABASE_URL", "sqlite:///:memory:")

from unittest.mock import MagicMock, patch

import pytest

from src.plugins.translate.preview_resolve_provider import resolve_provider_model


class TestResolveProviderModel:
    """resolve_provider_model — resolve LLM model name for job."""

    def _make_job(self, provider_id: str | None = "provider-1") -> MagicMock:
        job = MagicMock()
        job.provider_id = provider_id
        return job

    def test_with_provider_and_default_model(self):
        """Provider with default_model returns it."""
        db = MagicMock()
        job = self._make_job("provider-1")

        mock_provider = MagicMock()
        mock_provider.default_model = "gpt-4o"

        with patch("src.services.llm_provider.LLMProviderService") as MockCls:
            mock_service = MagicMock()
            mock_service.get_provider.return_value = mock_provider
            MockCls.return_value = mock_service
            result = resolve_provider_model(db, job)

        assert result == "gpt-4o"

    def test_with_provider_no_default_model_returns_fallback(self):
        """Provider with None default_model returns 'gpt-4o-mini'."""
        db = MagicMock()
        job = self._make_job("provider-1")

        mock_provider = MagicMock()
        mock_provider.default_model = None

        with patch("src.services.llm_provider.LLMProviderService") as MockCls:
            mock_service = MagicMock()
            mock_service.get_provider.return_value = mock_provider
            MockCls.return_value = mock_service
            result = resolve_provider_model(db, job)

        assert result == "gpt-4o-mini"

    def test_no_provider_id(self):
        """No provider_id returns None."""
        job = self._make_job(None)
        result = resolve_provider_model(MagicMock(), job)
        assert result is None

    def test_exception_during_resolve_returns_none(self):
        """Exception during provider resolve returns None."""
        db = MagicMock()
        job = self._make_job("provider-1")

        with patch("src.services.llm_provider.LLMProviderService", side_effect=Exception("DB error")):
            result = resolve_provider_model(db, job)

        assert result is None

    def test_provider_not_found_returns_none(self):
        """Provider not found (get_provider returns None) -> returns None."""
        db = MagicMock()
        job = self._make_job("provider-1")

        with patch("src.services.llm_provider.LLMProviderService") as MockCls:
            mock_service = MagicMock()
            mock_service.get_provider.return_value = None
            MockCls.return_value = mock_service
            result = resolve_provider_model(db, job)

        assert result is None

    def test_provider_service_initialized_with_db(self):
        """LLMProviderService initialized with the given db session."""
        db = MagicMock()
        job = self._make_job("provider-1")

        with patch("src.services.llm_provider.LLMProviderService") as MockCls:
            mock_service = MagicMock()
            mock_service.get_provider.return_value = MagicMock(default_model="claude-3-5-sonnet")
            MockCls.return_value = mock_service
            resolve_provider_model(db, job)

        MockCls.assert_called_once_with(db)
# #endregion Test.PreviewResolveProvider
