"""
Full coverage tests for document_loaders/loader_registry.py.

Targets uncovered paths:
- get_loader_for_path: unsupported extension returns None
- Conditional registry entries (HAS_ODT_LOADER, etc.)
- get_supported_extensions returns correct list
"""


class TestGetLoaderForPathUnsupported:
    """Cover get_loader_for_path with unsupported extensions."""

    def test_unsupported_extension_returns_none(self, tmp_path):
        """Returns None for file with unsupported extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_for_path,
        )

        f = tmp_path / "data.xyz123"
        f.write_text("content")
        result = get_loader_for_path(f)
        assert result is None

    def test_no_extension_returns_none(self, tmp_path):
        """Returns None for file with no extension."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_for_path,
        )

        f = tmp_path / "Makefile"
        f.write_text("all: build")
        result = get_loader_for_path(f)
        assert result is None

    def test_string_path_input(self, tmp_path):
        """Accepts string path input."""
        from local_deep_research.document_loaders.loader_registry import (
            get_loader_for_path,
        )

        f = tmp_path / "test.txt"
        f.write_text("hello")
        result = get_loader_for_path(str(f))
        assert result is not None


class TestGetSupportedExtensions:
    """Cover get_supported_extensions."""

    def test_returns_list_of_strings(self):
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        extensions = get_supported_extensions()
        assert isinstance(extensions, list)
        assert all(isinstance(e, str) for e in extensions)
        assert all(e.startswith(".") for e in extensions)

    def test_contains_required_extensions(self):
        from local_deep_research.document_loaders.loader_registry import (
            get_supported_extensions,
        )

        extensions = get_supported_extensions()
        for required in [".pdf", ".txt", ".md", ".csv", ".json", ".yaml"]:
            assert required in extensions


class TestConditionalLoaderRegistration:
    """Test conditional loader registration flags."""

    def test_odt_loader_flag_is_bool(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_ODT_LOADER,
        )

        assert isinstance(HAS_ODT_LOADER, bool)

    def test_epub_loader_flag_is_bool(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_EPUB_LOADER,
        )

        assert isinstance(HAS_EPUB_LOADER, bool)

    def test_rtf_loader_flag_is_bool(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_RTF_LOADER,
        )

        assert isinstance(HAS_RTF_LOADER, bool)

    def test_rst_loader_flag_is_bool(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_RST_LOADER,
        )

        assert isinstance(HAS_RST_LOADER, bool)

    def test_org_loader_flag_is_bool(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_ORG_LOADER,
        )

        assert isinstance(HAS_ORG_LOADER, bool)

    def test_image_loader_flag_is_bool(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_IMAGE_LOADER,
        )

        assert isinstance(HAS_IMAGE_LOADER, bool)

    def test_odt_in_registry_iff_flag_and_dep(self):
        # ODT needs both the loader class and python-docx (the partitioner
        # imports python-docx and shells out to pandoc).
        from local_deep_research.document_loaders.loader_registry import (
            HAS_DOCX_DEP,
            HAS_ODT_LOADER,
            LOADER_REGISTRY,
        )

        assert (".odt" in LOADER_REGISTRY) == (HAS_ODT_LOADER and HAS_DOCX_DEP)

    def test_epub_in_registry_iff_flag_and_dep(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_EPUB_LOADER,
            HAS_PANDOC_DEP,
            LOADER_REGISTRY,
        )

        assert (".epub" in LOADER_REGISTRY) == (
            HAS_EPUB_LOADER and HAS_PANDOC_DEP
        )

    def test_rtf_in_registry_iff_flag_and_dep(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_PANDOC_DEP,
            HAS_RTF_LOADER,
            LOADER_REGISTRY,
        )

        assert (".rtf" in LOADER_REGISTRY) == (
            HAS_RTF_LOADER and HAS_PANDOC_DEP
        )

    def test_rst_in_registry_iff_flag_and_dep(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_PANDOC_DEP,
            HAS_RST_LOADER,
            LOADER_REGISTRY,
        )

        assert (".rst" in LOADER_REGISTRY) == (
            HAS_RST_LOADER and HAS_PANDOC_DEP
        )

    def test_org_in_registry_iff_flag_and_dep(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_ORG_LOADER,
            HAS_PANDOC_DEP,
            LOADER_REGISTRY,
        )

        assert (".org" in LOADER_REGISTRY) == (
            HAS_ORG_LOADER and HAS_PANDOC_DEP
        )

    def test_image_extensions_in_registry_iff_flag_and_dep(self):
        from local_deep_research.document_loaders.loader_registry import (
            HAS_IMAGE_LOADER,
            HAS_OCR_DEP,
            LOADER_REGISTRY,
        )

        image_exts = [".png", ".jpg", ".jpeg", ".tiff", ".tif", ".bmp", ".heic"]
        for ext in image_exts:
            assert (ext in LOADER_REGISTRY) == (
                HAS_IMAGE_LOADER and HAS_OCR_DEP
            )


class TestIsExtensionSupportedCaseInsensitive:
    """Cover case-insensitive extension checking."""

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

        assert is_extension_supported(".PDF") is True

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

        assert is_extension_supported("Html") is True

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

        assert is_extension_supported(".JSON") is True
