import pytest

from clarabot_nano.common.solr.reflect import (
    SolrSchemaLanguageReflector,
    SolrSchemaReflectionError,
    SolrSchemaHighlightReflector,
)


def test_solr_reflector_language():
    r = SolrSchemaLanguageReflector()
    for t in [{}, {"fields": None}]:
        assert r.reflected is False
        with pytest.raises(SolrSchemaReflectionError, match="Field definitions are not found"):
            r.reflect_solr_schema(t)
    assert r.reflected is False

    for t in [{"fields": []}, {"fields": [{"name": "summary_faba"}]}, {"fields": [{"name": "text_faba"}]}]:
        assert r.reflected is False
        with pytest.raises(SolrSchemaReflectionError, match="Fallback fields are missing"):
            r.reflect_solr_schema(t)
    assert r.reflected is False

    for t in [
        {"fields": [{"name": "summary_faba"}, {"name": "text_faba"}, {"name": "summary_faba_ex"}]},
        {"fields": [{"name": "summary_faba"}, {"name": "text_faba"}, {"name": "text_faba_ex"}]},
    ]:
        assert r.reflected is False
        with pytest.raises(SolrSchemaReflectionError, match="Fallback fields may not have exact variants"):
            r.reflect_solr_schema(t)
    assert r.reflected is False

    r.reflect_solr_schema({"fields": [{"name": "summary_faba"}, {"name": "text_faba"}]})
    assert r.reflected is True
    assert r.languages == []

    r = SolrSchemaLanguageReflector()
    r.reflect_solr_schema(
        {"fields": [{"name": "summary_faba"}, {"name": "text_faba"}, {"name": "summary_hu"}, {"name": "text_hu"}]}
    )
    assert r.reflected is True
    assert r.languages == []

    r = SolrSchemaLanguageReflector()
    r.reflect_solr_schema(
        {
            "fields": [
                {"name": "summary_faba"},
                {"name": "text_faba"},
                {"name": "summary_hu"},
                {"name": "text_hu"},
                {"name": "summary_hu_ex"},
                {"name": "text_hu_ex"},
            ]
        }
    )
    assert r.reflected is True
    assert r.languages == ["hu"]

    r = SolrSchemaLanguageReflector()
    r.reflect_solr_schema(
        {
            "fields": [
                {"name": "summary_faba"},
                {"name": "text_faba"},
                {"name": "summary_hu"},
                {"name": "text_hu"},
                {"name": "summary_hu_ex"},
                {"name": "text_hu_ex"},
                {"name": "summary_en"},
                {"name": "text_en"},
                {"name": "summary_en_ex"},
                {"name": "text_en_ex"},
            ]
        }
    )
    assert r.reflected is True
    assert r.languages == ["en", "hu"]


def test_solr_reflector_highlight():
    r = SolrSchemaHighlightReflector()

    for t in [
        {"fields": [{"name": "summary_faba"}, {"name": "text_faba"}]},
        {"fields": [{"name": "summary_faba"}, {"name": "text_faba", "storeOffsetsWithPositions": True}]},
        {"fields": [{"name": "summary_faba"}, {"name": "text_faba", "stored": True}]},
    ]:
        assert r.reflected is False
        with pytest.raises(SolrSchemaReflectionError, match="Fallback has no highlight field"):
            r.reflect_solr_schema(t)

    r.reflect_solr_schema(
        {"fields": [{"name": "summary_faba"}, {"name": "text_faba", "storeOffsetsWithPositions": True, "stored": True}]}
    )
    assert r.reflected is True
    assert r.languages == []
    assert r.highlight_field_by_language == {"faba": "text_faba"}

    r = SolrSchemaHighlightReflector()
    with pytest.raises(SolrSchemaReflectionError, match="Language has no highlight field"):
        r.reflect_solr_schema(
            {
                "fields": [
                    {"name": "summary_faba"},
                    {"name": "text_faba", "storeOffsetsWithPositions": True, "stored": True},
                    {"name": "summary_hu"},
                    {"name": "text_hu"},
                    {"name": "summary_hu_ex"},
                    {"name": "text_hu_ex"},
                ]
            }
        )
    assert r.reflected is False

    r.reflect_solr_schema(
        {
            "fields": [
                {"name": "summary_faba"},
                {"name": "text_faba", "storeOffsetsWithPositions": True, "stored": True},
                {"name": "summary_hu"},
                {"name": "text_hu", "storeOffsetsWithPositions": True, "stored": True},
                {"name": "summary_hu_ex"},
                {"name": "text_hu_ex"},
            ]
        }
    )
    assert r.reflected is True
    assert r.languages == ["hu"]
    assert r.highlight_field_by_language == {"faba": "text_faba", "hu": "text_hu"}

    r = SolrSchemaHighlightReflector()
    with pytest.raises(
        SolrSchemaReflectionError, match="Multiple highlight fields for the same language is not necessary"
    ):
        r.reflect_solr_schema(
            {
                "fields": [
                    {"name": "summary_faba"},
                    {"name": "text_faba", "storeOffsetsWithPositions": True, "stored": True},
                    {"name": "summary_hu"},
                    {"name": "text_hu", "storeOffsetsWithPositions": True, "stored": True},
                    {"name": "summary_hu_ex"},
                    {"name": "text_hu_ex", "storeOffsetsWithPositions": True, "stored": True},
                ]
            }
        )
    assert r.reflected is False
