"""Tests for package installation and module surface.

These tests verify that:
1. The package can be imported
2. All 6 exports are present with correct types
3. __version__ exists
"""

import sys


def test_import_secbulkdownload() -> None:
    """Test that the root package can be imported."""
    import secbulkdownload

    assert secbulkdownload is not None


def test_all_six_exports_present() -> None:
    """Test that all 6 exports are present and accessible."""
    import secbulkdownload

    # Check all 6 exports
    assert hasattr(secbulkdownload, "idx")
    assert hasattr(secbulkdownload, "headerfile")
    assert hasattr(secbulkdownload, "filingArchive")
    assert hasattr(secbulkdownload, "filingDocument")
    assert hasattr(secbulkdownload, "rate_limiter")
    assert hasattr(secbulkdownload, "get_ticker_name_dicts")


def test_idx_is_class() -> None:
    """Test that idx is a class."""
    from secbulkdownload import idx

    assert isinstance(idx, type)


def test_headerfile_is_class() -> None:
    """Test that headerfile is a class."""
    from secbulkdownload import headerfile

    assert isinstance(headerfile, type)


def test_filingArchive_is_class() -> None:
    """Test that filingArchive is a class."""
    from secbulkdownload import filingArchive

    assert isinstance(filingArchive, type)


def test_filingDocument_is_class() -> None:
    """Test that filingDocument is a class."""
    from secbulkdownload import filingDocument

    assert isinstance(filingDocument, type)


def test_rate_limiter_is_class() -> None:
    """Test that rate_limiter is a class."""
    from secbulkdownload import rate_limiter

    assert isinstance(rate_limiter, type)


def test_get_ticker_name_dicts_is_callable() -> None:
    """Test that get_ticker_name_dicts is a callable function."""
    from secbulkdownload import get_ticker_name_dicts

    assert callable(get_ticker_name_dicts)


def test_version_exists() -> None:
    """Test that __version__ exists and is a string."""
    from secbulkdownload import __version__

    assert isinstance(__version__, str)
    assert len(__version__) > 0


def test_version_format() -> None:
    """Test that __version__ follows semantic versioning."""
    from secbulkdownload import __version__

    # Should be X.Y.Z format
    parts = __version__.split(".")
    assert len(parts) >= 2  # At least major.minor
    assert all(p.isdigit() for p in parts), f"Version {__version__} should be numeric parts"


def test_all_exports_in_all_list() -> None:
    """Test that all 6 exports are in __all__."""
    import secbulkdownload

    expected_exports = {
        "idx",
        "headerfile",
        "filingArchive",
        "filingDocument",
        "rate_limiter",
        "get_ticker_name_dicts",
    }

    assert hasattr(secbulkdownload, "__all__")
    assert set(secbulkdownload.__all__) == expected_exports


def test_no_pandas_dependency() -> None:
    """Test that pandas is NOT imported by our package.

    This verifies the clean migration to DuckDB.
    """
    # Ensure secbulkdownload has been imported
    import secbulkdownload  # noqa: F401

    # pandas should not be in sys.modules after our imports
    assert "pandas" not in sys.modules, (
        "pandas should not be imported by secbulkdownload - use DuckDB instead"
    )


def test_no_polars_dependency() -> None:
    """Test that polars is NOT imported by our package.

    This verifies the simplified plain Python data flow.
    """
    # Ensure secbulkdownload has been imported
    import secbulkdownload  # noqa: F401

    # polars should not be in sys.modules after our imports
    assert "polars" not in sys.modules, (
        "polars should not be imported by secbulkdownload - use plain Python data structures"
    )


def test_idx_instantiation() -> None:
    """Test that idx class can be instantiated with required arguments."""
    from secbulkdownload import idx

    instance = idx(user_agent="Test Company test@test.com")
    assert instance is not None
    assert instance.user_agent == "Test Company test@test.com"


def test_headerfile_instantiation() -> None:
    """Test that headerfile class can be instantiated."""
    from secbulkdownload import headerfile

    instance = headerfile(
        sec_filename="edgar/data/0000320193/0000320193-23-000077.txt",
        user_agent="Test Company test@test.com",
    )
    assert instance is not None


def test_rate_limiter_instantiation() -> None:
    """Test that rate_limiter can be instantiated."""
    from secbulkdownload import rate_limiter

    limiter = rate_limiter(interval=1.0, limit=10)
    assert limiter is not None
    assert limiter.interval == 1.0
    assert limiter.limit == 10


def test_filingDocument_instantiation() -> None:
    """Test that filingDocument class can be instantiated."""
    from secbulkdownload import filingDocument

    doc = filingDocument(
        filename="test.txt",
        file_type="10-K",
        sequence="1",
        description="Annual report",
        body="Test content",
        html="<html>Test content</html>",
    )
    assert doc is not None
    assert doc.filename == "test.txt"
    assert doc.type == "10-K"


def test_filingArchive_instantiation() -> None:
    """Test that filingArchive class can be instantiated."""
    from unittest.mock import patch

    from secbulkdownload import filingArchive

    with patch("secbulkdownload.secfiling.headerfile") as mock_headerfile:
        mock_headerfile.return_value.get_headerDict.return_value = {}

        instance = filingArchive(
            sec_filepath="edgar/data/0000320193/0000320193-23-000077.txt",
            user_agent="Test Company test@test.com",
        )
        assert instance is not None


def test_get_ticker_name_dicts_returns_tuple() -> None:
    """Test that get_ticker_name_dicts returns tuple of two dicts."""
    from secbulkdownload import get_ticker_name_dicts

    result = get_ticker_name_dicts()
    assert isinstance(result, tuple)
    assert len(result) == 2
    assert isinstance(result[0], dict)
    assert isinstance(result[1], dict)
