"""
Coverage tests for verify_signature.py - untested branches.
"""

from unittest.mock import Mock

import pytest

# ========================================================================
# verify_signature function branches
# ========================================================================


def test_verify_signature_with_valid_data():
    """Test verify_signature with valid signature data."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        args = {
            "data": b"test data",
            "signature": b"signature",
            "protocolID": {"securityLevel": 2, "protocol": "test"},
            "keyID": "1",
        }

        wallet = Mock()
        wallet.verify_signature.return_value = {"valid": True}

        result = verify_signature(wallet, args, "origin")
        assert result is not None
    except ImportError:
        pytest.skip("verify_signature not available")


def test_verify_signature_with_missing_data():
    """Test verify_signature with missing data field."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        args = {"signature": b"signature", "protocolID": [2, "test"]}

        wallet = Mock()

        try:
            result = verify_signature(wallet, args, "origin")
            assert result is not None
        except (KeyError, ValueError):
            # Expected - exception caught
            pass
    except ImportError:
        pytest.skip("verify_signature not available")


def test_verify_signature_with_missing_signature():
    """Test verify_signature with missing signature field."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        args = {"data": b"test data", "protocolID": [2, "test"]}

        wallet = Mock()

        try:
            result = verify_signature(wallet, args, "origin")
            assert result is not None
        except (KeyError, ValueError):
            # Expected - exception caught
            pass
    except ImportError:
        pytest.skip("verify_signature not available")


def test_verify_signature_with_none_protocol_id():
    """Test verify_signature with None protocolID."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        args = {"data": b"test data", "signature": b"signature", "protocolID": None}

        wallet = Mock()
        wallet.verify_signature.return_value = {"valid": False}

        result = verify_signature(wallet, args, "origin")
        assert result is not None
    except ImportError:
        pytest.skip("verify_signature not available")


def test_verify_signature_with_string_protocol_id():
    """Test verify_signature with string protocolID."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        args = {"data": b"test data", "signature": b"signature", "protocolID": "test_protocol"}

        wallet = Mock()
        wallet.verify_signature.return_value = {"valid": True}

        result = verify_signature(wallet, args, "origin")
        assert result is not None
    except ImportError:
        pytest.skip("verify_signature not available")


def test_verify_signature_with_empty_data():
    """Test verify_signature with empty data."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        args = {"data": b"", "signature": b"signature", "protocolID": [2, "test"]}

        wallet = Mock()
        wallet.verify_signature.return_value = {"valid": False}

        result = verify_signature(wallet, args, "origin")
        assert result is not None
    except ImportError:
        pytest.skip("verify_signature not available")


def test_verify_signature_with_counterparty():
    """Test verify_signature with counterparty parameter."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        args = {
            "data": b"test data",
            "signature": b"signature",
            "protocolID": {"securityLevel": 2, "protocol": "test"},
            "counterparty": "self",
        }

        wallet = Mock()
        wallet.verify_signature.return_value = {"valid": True}

        result = verify_signature(wallet, args, "origin")
        assert result is not None
    except ImportError:
        pytest.skip("verify_signature not available")


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


def test_verify_signature_with_none_args():
    """Test verify_signature with None args."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        wallet = Mock()

        try:
            result = verify_signature(wallet, None, "origin")
            assert result is not None
        except (TypeError, AttributeError):
            # Expected - exception caught
            pass
    except ImportError:
        pytest.skip("verify_signature not available")


def test_verify_signature_with_empty_args():
    """Test verify_signature with empty args."""
    try:
        from bsv.wallet.serializer.verify_signature import verify_signature

        wallet = Mock()

        try:
            result = verify_signature(wallet, {}, "origin")
            assert result is not None
        except (KeyError, ValueError):
            # Expected - exception caught
            pass
    except ImportError:
        pytest.skip("verify_signature not available")
