"""Regression tests: LibraryRAGSearchEngine must read its RAG settings
from the provided ``settings_snapshot``.

The constructor used to pass ``(key, settings_snapshot, default)``
positionally into ``get_setting_from_snapshot(key, default, username,
settings_snapshot)``, so the snapshot landed in the ``default`` slot and
was never consulted: outside a thread settings context each setting
silently resolved to the snapshot DICT itself.
"""

from local_deep_research.web_search_engines.engines.search_engine_library import (
    LibraryRAGSearchEngine,
)


def test_snapshot_values_are_honored():
    snap = {
        "_username": "tester",
        "local_search_embedding_model": "custom-model",
        "local_search_embedding_provider": "ollama",
        "local_search_chunk_size": 512,
        "local_search_chunk_overlap": 64,
    }
    eng = LibraryRAGSearchEngine(settings_snapshot=snap)
    assert eng.embedding_model == "custom-model"
    assert eng.embedding_provider == "ollama"
    assert eng.chunk_size == 512
    assert eng.chunk_overlap == 64


def test_defaults_used_when_keys_missing():
    eng = LibraryRAGSearchEngine(settings_snapshot={"_username": "tester"})
    assert eng.embedding_model == "all-MiniLM-L6-v2"
    assert eng.embedding_provider == "sentence_transformers"
    assert eng.chunk_size == 1000
    assert eng.chunk_overlap == 200


# ---------------------------------------------------------------------------
# search.rag.max_results — cap local RAG retrieval (vector search returns many
# near-duplicate chunks that otherwise flood the agent's context).
# search.rag.enable_relevance_filter — opt-in LLM relevance filter for RAG.
# ---------------------------------------------------------------------------


def test_rag_max_results_caps_retrieval_when_set():
    """search.rag.max_results overrides the inherited (web) max_results so
    local RAG search returns fewer chunks."""
    eng = LibraryRAGSearchEngine(
        max_results=50,  # global web default
        settings_snapshot={"_username": "tester", "search.rag.max_results": 15},
    )
    assert eng.max_results == 15


def test_rag_max_results_absent_preserves_caller_value():
    """An absent setting must NOT silently cap a caller's explicit
    max_results; real web/CLI runs carry the shipped default of 20."""
    eng = LibraryRAGSearchEngine(
        max_results=42,
        settings_snapshot={"_username": "tester"},
    )
    assert eng.max_results == 42


def test_rag_relevance_filter_on_by_default():
    eng = LibraryRAGSearchEngine(settings_snapshot={"_username": "tester"})
    assert eng.enable_llm_relevance_filter is True


def test_rag_relevance_filter_can_be_disabled():
    eng = LibraryRAGSearchEngine(
        settings_snapshot={
            "_username": "tester",
            "search.rag.enable_relevance_filter": False,
        },
    )
    assert eng.enable_llm_relevance_filter is False


def test_collection_engine_inherits_rag_cap_and_filter():
    """CollectionSearchEngine (subclass) gets the cap + filter from the base
    __init__, which runs before its DB-loading collection-settings step."""
    from unittest.mock import patch

    from local_deep_research.web_search_engines.engines.search_engine_collection import (
        CollectionSearchEngine,
    )

    with patch.object(
        CollectionSearchEngine, "_load_collection_embedding_settings"
    ):
        eng = CollectionSearchEngine(
            collection_id="abc",
            collection_name="My Collection",
            max_results=50,
            settings_snapshot={
                "_username": "tester",
                "search.rag.max_results": 12,
                "search.rag.enable_relevance_filter": False,
            },
        )
    assert eng.max_results == 12
    assert eng.enable_llm_relevance_filter is False
