"""
Coverage tests for chaintrackers/ modules - untested branches.
"""

import pytest

# Constants for skip messages
SKIP_WOC_TRACKER = "WhatsOnChainTracker not available"


# ========================================================================
# WhatsOnChain chaintracker branches
# ========================================================================


def test_woc_chaintracker_init():
    """Test WhatsOnChain chaintracker initialization."""
    try:
        from bsv.chaintrackers import WhatsOnChainTracker

        tracker = WhatsOnChainTracker()
        assert tracker is not None
    except (ImportError, AttributeError):
        pytest.skip(SKIP_WOC_TRACKER)


def test_woc_chaintracker_with_network():
    """Test WhatsOnChain chaintracker with network."""
    try:
        from bsv.chaintrackers import WhatsOnChainTracker

        tracker = WhatsOnChainTracker(network="mainnet")
        assert tracker is not None
    except (ImportError, AttributeError, TypeError):
        pytest.skip("WhatsOnChainTracker not available or different signature")


def test_woc_chaintracker_get_height():
    """Test getting chain height."""
    try:
        from bsv.chaintrackers import WhatsOnChainTracker

        tracker = WhatsOnChainTracker()

        if hasattr(tracker, "get_height"):
            try:
                height = tracker.get_height()
                assert isinstance(height, int)
            except Exception:
                # Expected without network access
                pytest.skip("Requires network access")
    except (ImportError, AttributeError):
        pytest.skip(SKIP_WOC_TRACKER)


def test_woc_chaintracker_get_header():
    """Test getting block header."""
    try:
        from bsv.chaintrackers import WhatsOnChainTracker

        tracker = WhatsOnChainTracker()

        if hasattr(tracker, "get_header"):
            try:
                tracker.get_header(0)  # Genesis
                # Header retrieved or exception raised
            except Exception:
                # Expected without network access
                pytest.skip("Requires network access")
    except (ImportError, AttributeError):
        pytest.skip(SKIP_WOC_TRACKER)


# ========================================================================
# Headers client chaintracker branches
# ========================================================================


def test_headers_client_chaintracker():
    """Test headers client chaintracker."""
    try:
        from bsv.chaintrackers import HeadersClientTracker

        try:
            tracker = HeadersClientTracker()
            assert tracker is not None
        except TypeError:
            # May require parameters
            pytest.skip("HeadersClientTracker requires parameters")
    except (ImportError, AttributeError):
        pytest.skip("HeadersClientTracker not available")


# ========================================================================
# Edge cases
# ========================================================================


def test_chaintracker_invalid_height():
    """Test chaintracker with invalid height."""
    try:
        from bsv.chaintrackers import WhatsOnChainTracker

        tracker = WhatsOnChainTracker()

        if hasattr(tracker, "get_header"):
            try:
                tracker.get_header(-1)
            except ValueError:
                # Expected
                pass
    except (ImportError, AttributeError):
        pytest.skip(SKIP_WOC_TRACKER)
