"""
Deep coverage tests for loader_registry module.

Targets branches and code paths not exercised by the existing
test_loader_registry_coverage.py file:
- get_loader_for_path with extension-without-dot normalisation
- get_loader_class_for_extension with extension-without-dot path
- JSON / YAML kwargs correctness
- ipynb and enex kwargs
- registry completeness for always-present extensions
- is_extension_supported with dot-prefixed input
"""

from unittest.mock import patch

import pytest

MODULE = "local_deep_research.document_loaders.loader_registry"


# ---------------------------------------------------------------------------
# get_loader_class_for_extension – branch: no-dot input path
# ---------------------------------------------------------------------------


class TestGetLoaderClassForExtensionNoDot:
    """Exercise the branch where extension does NOT start with a dot."""

    def test_txt_without_dot_resolves(self):
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension("txt")
        assert result is not None
        loader_class, kwargs = result
        assert loader_class.__name__ == "TextLoader"

    def test_pdf_without_dot_resolves(self):
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension("pdf")
        assert result is not None
        assert result[0].__name__ == "PyPDFLoader"

    def test_json_without_dot_resolves(self):
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        result = get_loader_class_for_extension("json")
        assert result is not None
        assert result[0].__name__ == "SimpleJSONLoader"

    def test_unknown_without_dot_returns_none(self):
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_class_for_extension,
        )

        assert get_loader_class_for_extension("zzz_unknown") is None


# ---------------------------------------------------------------------------
# is_extension_supported – branch: no-dot input path
# ---------------------------------------------------------------------------


class TestIsExtensionSupportedNoDotBranch:
    """Exercise the branch that prepends a dot when missing."""

    def test_csv_without_dot(self):
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported("csv") is True

    def test_md_without_dot_uppercase(self):
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported("MD") is True

    def test_unknown_without_dot(self):
        from local_deep_research.document_loaders.loader_registry import (
            is_extension_supported,
        )

        assert is_extension_supported("zzz") is False


# ---------------------------------------------------------------------------
# get_loader_for_path – loader constructor exception path
# ---------------------------------------------------------------------------


class TestGetLoaderForPathExceptionHandling:
    """Verify that get_loader_for_path returns None when the loader raises."""

    def test_constructor_runtime_error_returns_none(self, tmp_path):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
            get_loader_for_path,
        )

        class _BrokenLoader:
            def __init__(self, *args, **kwargs):
                raise RuntimeError("constructor broken")

        f = tmp_path / "test.toml"
        f.write_text("[key]\nvalue = 1")

        with patch.dict(
            LOADER_REGISTRY,
            {".toml": {"loader_class": _BrokenLoader, "loader_kwargs": {}}},
        ):
            result = get_loader_for_path(f)
        assert result is None

    def test_constructor_value_error_returns_none(self, tmp_path):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
            get_loader_for_path,
        )

        class _ValErrLoader:
            def __init__(self, *args, **kwargs):
                raise ValueError("bad value")

        f = tmp_path / "sample.eml"
        f.write_text("From: a@b.com\n\nHello")

        with patch.dict(
            LOADER_REGISTRY,
            {".eml": {"loader_class": _ValErrLoader, "loader_kwargs": {}}},
        ):
            result = get_loader_for_path(f)
        assert result is None


# ---------------------------------------------------------------------------
# Always-present extensions
# ---------------------------------------------------------------------------


class TestAlwaysPresentExtensions:
    """Verify extensions that must exist regardless of optional loader flags."""

    @pytest.mark.parametrize(
        "ext",
        [
            ".pdf",
            ".txt",
            ".md",
            ".markdown",
            ".docx",
            ".csv",
            ".xlsx",
            ".xls",
            ".html",
            ".htm",
            ".pptx",
            ".xml",
            ".eml",
            ".tsv",
            ".json",
            ".yaml",
            ".yml",
            ".ipynb",
            ".enex",
            ".toml",
            ".mhtml",
            ".mht",
        ],
    )
    def test_extension_always_registered(self, ext):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        assert ext in LOADER_REGISTRY, f"{ext} not in LOADER_REGISTRY"


# ---------------------------------------------------------------------------
# Kwargs deep checks
# ---------------------------------------------------------------------------


class TestSpecificKwargsDeep:
    """Deeper checks on loader kwargs not covered by existing tests."""

    def test_json_loader_kwargs_empty(self):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        assert LOADER_REGISTRY[".json"]["loader_kwargs"] == {}

    def test_yaml_loader_kwargs_empty(self):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        assert LOADER_REGISTRY[".yaml"]["loader_kwargs"] == {}

    def test_yml_loader_kwargs_empty(self):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        assert LOADER_REGISTRY[".yml"]["loader_kwargs"] == {}

    def test_enex_single_document_false(self):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        entry = LOADER_REGISTRY[".enex"]
        assert entry["loader_kwargs"].get("load_single_document") is False

    def test_mhtml_kwargs_empty(self):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        assert LOADER_REGISTRY[".mhtml"]["loader_kwargs"] == {}

    def test_mht_kwargs_empty(self):
        from local_deep_research.document_loaders.loader_registry import (
            LOADER_REGISTRY,
        )

        assert LOADER_REGISTRY[".mht"]["loader_kwargs"] == {}
