"""Tests for ExporterRegistry."""


class TestExporterRegistryBasics:
    """Basic tests for ExporterRegistry functionality."""

    def test_registry_has_exporters_registered(self):
        """Test that exporters are automatically registered on import."""
        from local_deep_research.exporters import ExporterRegistry

        formats = ExporterRegistry.get_available_formats()

        # Should have all core formats
        assert "pdf" in formats
        assert "odt" in formats
        assert "latex" in formats
        assert "quarto" in formats
        assert "ris" in formats

    def test_get_exporter_returns_instance(self):
        """Test that get_exporter returns an exporter instance."""
        from local_deep_research.exporters import ExporterRegistry, BaseExporter

        exporter = ExporterRegistry.get_exporter("pdf")

        assert exporter is not None
        assert isinstance(exporter, BaseExporter)

    def test_get_exporter_returns_none_for_unknown_format(self):
        """Test that get_exporter returns None for unknown formats."""
        from local_deep_research.exporters import ExporterRegistry

        exporter = ExporterRegistry.get_exporter("unknown_format")

        assert exporter is None

    def test_is_format_supported_returns_true_for_known_format(self):
        """Test is_format_supported for known formats."""
        from local_deep_research.exporters import ExporterRegistry

        assert ExporterRegistry.is_format_supported("pdf") is True
        assert ExporterRegistry.is_format_supported("odt") is True
        assert ExporterRegistry.is_format_supported("latex") is True

    def test_is_format_supported_returns_false_for_unknown_format(self):
        """Test is_format_supported for unknown formats."""
        from local_deep_research.exporters import ExporterRegistry

        assert ExporterRegistry.is_format_supported("unknown") is False
        assert ExporterRegistry.is_format_supported("xyz") is False

    def test_format_names_are_case_insensitive(self):
        """Test that format names are case insensitive."""
        from local_deep_research.exporters import ExporterRegistry

        # Should work with different cases
        assert ExporterRegistry.is_format_supported("PDF") is True
        assert ExporterRegistry.is_format_supported("Pdf") is True
        assert ExporterRegistry.is_format_supported("pdf") is True

        # get_exporter should also be case insensitive
        assert ExporterRegistry.get_exporter("PDF") is not None
        assert ExporterRegistry.get_exporter("Pdf") is not None


class TestExporterRegistrySingleton:
    """Tests for singleton behavior of registered exporters."""

    def test_get_exporter_returns_same_instance(self):
        """Test that get_exporter returns the same instance."""
        from local_deep_research.exporters import ExporterRegistry

        exporter1 = ExporterRegistry.get_exporter("pdf")
        exporter2 = ExporterRegistry.get_exporter("pdf")

        assert exporter1 is exporter2

    def test_different_formats_return_different_instances(self):
        """Test that different formats return different exporters."""
        from local_deep_research.exporters import ExporterRegistry

        pdf_exporter = ExporterRegistry.get_exporter("pdf")
        latex_exporter = ExporterRegistry.get_exporter("latex")

        assert pdf_exporter is not latex_exporter


class TestExporterRegistryCustomRegistration:
    """Tests for custom exporter registration."""

    def test_register_decorator_works(self):
        """Test that @register decorator works for new exporters."""
        from local_deep_research.exporters import (
            ExporterRegistry,
            BaseExporter,
            ExportResult,
        )

        # Create a custom exporter
        @ExporterRegistry.register
        class TestExporter(BaseExporter):
            @property
            def format_name(self) -> str:
                return "test_format"

            @property
            def file_extension(self) -> str:
                return ".test"

            @property
            def mimetype(self) -> str:
                return "application/x-test"

            def export(self, markdown_content, options=None):
                return ExportResult(
                    content=b"test",
                    filename="test.test",
                    mimetype=self.mimetype,
                )

        # Should be registered
        assert ExporterRegistry.is_format_supported("test_format")
        exporter = ExporterRegistry.get_exporter("test_format")
        assert exporter is not None
        assert exporter.format_name == "test_format"

        # Clean up - remove from registry
        if "test_format" in ExporterRegistry._exporters:
            del ExporterRegistry._exporters["test_format"]
        if "test_format" in ExporterRegistry._instances:
            del ExporterRegistry._instances["test_format"]
