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

import pytest

# Constants for skip messages
SKIP_AES_CBC = "AES-CBC not available"


# ========================================================================
# AES-CBC encryption branches
# ========================================================================


def test_aes_cbc_encrypt_empty():
    """Test AES-CBC encryption with empty data."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 32  # 256-bit key
        encrypted = encrypt(b"", key)
        assert isinstance(encrypted, bytes)
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


def test_aes_cbc_encrypt_small():
    """Test AES-CBC encryption with small data."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 32
        encrypted = encrypt(b"test", key)
        assert isinstance(encrypted, bytes)
        assert len(encrypted) > 0
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


def test_aes_cbc_encrypt_block_size():
    """Test AES-CBC encryption with block-sized data."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 32
        data = b"\x00" * 16  # AES block size
        encrypted = encrypt(data, key)
        assert isinstance(encrypted, bytes)
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


def test_aes_cbc_encrypt_large():
    """Test AES-CBC encryption with large data."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 32
        data = b"x" * 10000
        encrypted = encrypt(data, key)
        assert isinstance(encrypted, bytes)
        assert len(encrypted) >= len(data)
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


# ========================================================================
# AES-CBC decryption branches
# ========================================================================


def test_aes_cbc_decrypt_valid():
    """Test AES-CBC decryption with valid data."""
    try:
        from bsv.aes_cbc import decrypt, encrypt

        key = b"\x00" * 32
        data = b"test message"

        encrypted = encrypt(data, key)
        decrypted = decrypt(encrypted, key)

        assert decrypted == data
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


def test_aes_cbc_decrypt_wrong_key():
    """Test AES-CBC decryption with wrong key."""
    try:
        from bsv.aes_cbc import decrypt, encrypt

        key1 = b"\x00" * 32
        key2 = b"\x01" * 32
        data = b"test"

        encrypted = encrypt(data, key1)
        decrypted = decrypt(encrypted, key2)

        # Should produce garbage or error
        assert decrypted != data
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


def test_aes_cbc_decrypt_invalid_data():
    """Test AES-CBC decryption with invalid data."""
    try:
        from bsv.aes_cbc import decrypt

        key = b"\x00" * 32

        try:
            _ = decrypt(b"invalid", key)
        except Exception:
            # Expected to fail
            pass
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


# ========================================================================
# IV (Initialization Vector) branches
# ========================================================================


def test_aes_cbc_with_custom_iv():
    """Test AES-CBC with custom IV."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 32
        iv = b"\x01" * 16  # AES IV is 16 bytes

        try:
            encrypted = encrypt(b"test", key, iv=iv)
            assert isinstance(encrypted, bytes)
        except TypeError:
            # encrypt may not accept IV parameter
            pytest.skip("encrypt doesn't support custom IV")
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


# ========================================================================
# Key size branches
# ========================================================================


def test_aes_cbc_128_bit_key():
    """Test AES-CBC with 128-bit key."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 16  # 128-bit
        encrypted = encrypt(b"test", key)
        assert isinstance(encrypted, bytes)
    except (ImportError, ValueError):
        pytest.skip("128-bit AES-CBC not available or not supported")


def test_aes_cbc_256_bit_key():
    """Test AES-CBC with 256-bit key."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 32  # 256-bit
        encrypted = encrypt(b"test", key)
        assert isinstance(encrypted, bytes)
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


def test_aes_cbc_invalid_key_size():
    """Test AES-CBC with invalid key size."""
    try:
        from bsv.aes_cbc import encrypt

        key = b"\x00" * 15  # Invalid size

        try:
            _ = encrypt(b"test", key)
        except ValueError:
            # Expected to fail
            pass
    except ImportError:
        pytest.skip(SKIP_AES_CBC)


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


def test_aes_cbc_roundtrip():
    """Test AES-CBC encryption/decryption roundtrip."""
    try:
        from bsv.aes_cbc import decrypt, encrypt

        key = b"\x01\x02\x03" * 10 + b"\x00\x00"  # 32 bytes
        original = b"roundtrip test data"

        encrypted = encrypt(original, key)
        decrypted = decrypt(encrypted, key)

        assert decrypted == original
    except ImportError:
        pytest.skip(SKIP_AES_CBC)
