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

import pytest

from bsv.script.script import Script
from bsv.transaction import Transaction
from bsv.transaction_input import TransactionInput
from bsv.transaction_output import TransactionOutput

# ========================================================================
# TransactionInput initialization branches
# ========================================================================


def test_transaction_input_init_with_txid():
    """Test TransactionInput with source_txid."""
    inp = TransactionInput(
        source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    assert inp.source_txid == "0" * 64


def test_transaction_input_init_with_transaction():
    """Test TransactionInput with source_transaction."""
    tx = Transaction(
        version=1, tx_inputs=[], tx_outputs=[TransactionOutput(satoshis=1000, locking_script=Script(b""))], locktime=0
    )

    inp = TransactionInput(
        source_transaction=tx, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    assert inp.source_transaction == tx


def test_transaction_input_init_with_none_source():
    """Test TransactionInput with None source."""
    try:
        inp = TransactionInput(
            source_txid=None, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
        )
        assert inp.source_txid is None
    except Exception:
        # May require source
        pass


def test_transaction_input_init_with_template():
    """Test TransactionInput with unlocking_script_template."""
    try:
        from bsv.script.unlocking_template import UnlockingScriptTemplate

        template = None  # Mock template
        inp = TransactionInput(
            source_txid="0" * 64, source_output_index=0, unlocking_script_template=template, sequence=0xFFFFFFFF
        )
        assert inp.unlocking_script_template == template
    except ImportError:
        pytest.skip("UnlockingScriptTemplate not available")


def test_transaction_input_init_zero_index():
    """Test TransactionInput with zero output index."""
    inp = TransactionInput(
        source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    assert inp.source_output_index == 0


def test_transaction_input_init_large_index():
    """Test TransactionInput with large output index."""
    inp = TransactionInput(
        source_txid="0" * 64, source_output_index=999, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    assert inp.source_output_index == 999


def test_transaction_input_init_empty_script():
    """Test TransactionInput with empty unlocking script."""
    inp = TransactionInput(
        source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    assert len(inp.unlocking_script.serialize()) == 0


def test_transaction_input_init_with_script():
    """Test TransactionInput with unlocking script."""
    inp = TransactionInput(
        source_txid="0" * 64,
        source_output_index=0,
        unlocking_script=Script(b"\x51"),
        sequence=0xFFFFFFFF,  # OP_1
    )
    assert len(inp.unlocking_script.serialize()) > 0


# ========================================================================
# Sequence number branches
# ========================================================================


def test_transaction_input_sequence_max():
    """Test TransactionInput with max sequence (0xFFFFFFFF)."""
    inp = TransactionInput(
        source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    assert inp.sequence == 0xFFFFFFFF


def test_transaction_input_sequence_zero():
    """Test TransactionInput with zero sequence."""
    inp = TransactionInput(source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=0)
    assert inp.sequence == 0


def test_transaction_input_sequence_custom():
    """Test TransactionInput with custom sequence."""
    inp = TransactionInput(source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=12345)
    assert inp.sequence == 12345


# ========================================================================
# Serialization branches
# ========================================================================


def test_transaction_input_serialize():
    """Test TransactionInput serialization."""
    inp = TransactionInput(
        source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    serialized = inp.serialize()
    assert isinstance(serialized, bytes)
    assert len(serialized) > 0


def test_transaction_input_serialize_with_script():
    """Test TransactionInput serialization with script."""
    inp = TransactionInput(
        source_txid="0" * 64,
        source_output_index=0,
        unlocking_script=Script(b"\x51\x52"),  # OP_1 OP_2
        sequence=0xFFFFFFFF,
    )
    serialized = inp.serialize()
    assert len(serialized) > 36  # prevout (36 bytes) + script + sequence


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


def test_transaction_input_str_representation():
    """Test TransactionInput string representation."""
    inp = TransactionInput(
        source_txid="0" * 64, source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
    )
    str_repr = str(inp)
    assert isinstance(str_repr, str)


def test_transaction_input_with_short_txid():
    """Test TransactionInput with short txid."""
    try:
        inp = TransactionInput(
            source_txid="abc", source_output_index=0, unlocking_script=Script(b""), sequence=0xFFFFFFFF
        )
        assert inp.source_txid == "abc"
    except ValueError:
        # May validate txid length
        pass
