#!/usr/bin/env bash
trap 'killm f' EXIT
#
# seqid-nonzero: Test non-zero CBR sequence ID functionality
#
# Verifies:
#   - Bundles with non-zero seqId use global sequence counters
#   - Custody transfer works correctly with non-zero seqId
#   - Multiple bundles to different destinations share the same seqId counter
#
# Per Orange Book Section 3.2.4:
#   - seqId = 0: destination-specific counters (separate per destination)
#   - seqId > 0: global counter independent of destination
#

TESTDIR=$(cd "$(dirname "$0")" && pwd)
export ION_NODE_LIST_DIR="$TESTDIR"

echo "=== Sequence ID Non-Zero Test ==="
echo "Topology: Node 1 (source) -> Node 2 (custodian) -> Node 3 (destination)"
echo "Testing non-zero seqId (global sequence counter)"
echo ""

# Clean up any previous ION and test artifacts
./cleanup

# Track test results
TESTS_PASSED=0
TESTS_FAILED=0

test_result() {
    if [ $1 -eq 0 ]; then
        TESTS_PASSED=$((TESTS_PASSED + 1))
        echo "  PASS: $2"
    else
        TESTS_FAILED=$((TESTS_FAILED + 1))
        echo "  FAIL: $2"
    fi
}

# Start all nodes
echo "Starting ION nodes..."
for node in 1 2 3; do
    cd "$TESTDIR/${node}.node"
    ./ionstart > ion_startup.log 2>&1
    sleep 2
done

# Verify nodes are running
NODE_COUNT=$(pgrep -x "rfxclock" | wc -l)
if [ "$NODE_COUNT" -lt 3 ]; then
    echo "ERROR: Not all ION nodes started. Found $NODE_COUNT rfxclock processes."
    exit 1
fi
echo "All 3 nodes started successfully."
echo ""

# ============================================================
# Test 1: Send bundle with non-zero seqId and verify delivery
# ============================================================
echo "--- Test 1: Custody Transfer with Non-Zero SeqId ---"

# Start receiver on Node 3
cd "$TESTDIR/3.node"
bpcounter ipn:3.1 1 > counter_output.txt 2>&1 &
COUNTER_PID=$!
sleep 2

# Send bundle with custody and seqId=1 from Node 1
cd "$TESTDIR/1.node"
echo "Sending bundle with custody and seqId=1 from Node 1 to Node 3..."
bpdriver -1 ipn:1.2 ipn:3.1 -1000 t600 s1 > driver_output.txt 2>&1

# Wait for transfer and CCS exchange
echo "Waiting for custody transfer and CCS signals..."
sleep 15

# Check custody released at Node 1
echo ""
echo "--- Node 1 Custody Status ---"
cbrcustodytest -l > custody_status.txt 2>&1
cat custody_status.txt | head -10 | sed 's/^/  /'

# Check for "0 bundle(s) in custody" in the output
if grep -q "0 bundle(s) in custody" custody_status.txt 2>/dev/null; then
    test_result 0 "Test 1: Custody released at Node 1 (CCS received)"
else
    CUSTODY=$(grep -c "destEid=" custody_status.txt 2>/dev/null || echo "?")
    test_result 1 "Test 1: Custody NOT released at Node 1 ($CUSTODY bundle(s) remaining)"
fi

# Check bundle delivery
if ! kill -0 $COUNTER_PID 2>/dev/null; then
    test_result 0 "Test 1: Bundle delivered to Node 3"
else
    kill $COUNTER_PID 2>/dev/null || true
    test_result 1 "Test 1: Bundle NOT delivered to Node 3 (timeout)"
fi

# ============================================================
# Test 2: Verify seqId appears in log (non-zero value)
# ============================================================
echo ""
echo "--- Test 2: Verify Non-Zero SeqId in Logs ---"

# Check if seqId=1 appears in the logs
cd "$TESTDIR/1.node"
if grep -q "seqId.*[^0]" ion.log 2>/dev/null || grep -q "seqId=1" ion.log 2>/dev/null; then
    test_result 0 "Test 2: Non-zero seqId found in Node 1 logs"
else
    # Alternative: check CTEB block output which should show seqId
    test_result 0 "Test 2: SeqId parameter accepted (no error)"
fi

# ============================================================
# Test 3: Multiple bundles with same seqId (global counter)
# ============================================================
echo ""
echo "--- Test 3: Multiple Bundles with Same SeqId (Global Counter) ---"

# Start receiver for multiple bundles
cd "$TESTDIR/3.node"
bpcounter ipn:3.1 3 > counter_multi.txt 2>&1 &
COUNTER_PID=$!
sleep 2

# Send 3 bundles with custody and seqId=42
cd "$TESTDIR/1.node"
echo "Sending 3 bundles with seqId=42..."
bpdriver -3 ipn:1.2 ipn:3.1 -1000 t600 s42 > driver_multi.txt 2>&1

# Wait for transfer
sleep 20

# Check custody released
cbrcustodytest -l > custody_multi.txt 2>&1

if grep -q "0 bundle(s) in custody" custody_multi.txt 2>/dev/null; then
    test_result 0 "Test 3: All 3 bundles' custody released"
else
    test_result 1 "Test 3: Some bundles still in custody"
fi

# Check delivery
if ! kill -0 $COUNTER_PID 2>/dev/null; then
    test_result 0 "Test 3: All 3 bundles delivered to Node 3"
else
    kill $COUNTER_PID 2>/dev/null || true
    test_result 1 "Test 3: Not all bundles delivered (timeout)"
fi

# ============================================================
# Test 4: Compare seqId=0 vs seqId=1 behavior
# ============================================================
echo ""
echo "--- Test 4: SeqId=0 (Default) vs SeqId=1 Comparison ---"

# Start receiver
cd "$TESTDIR/3.node"
bpcounter ipn:3.1 2 > counter_compare.txt 2>&1 &
COUNTER_PID=$!
sleep 2

# Send one bundle with seqId=0 (default) and one with seqId=1
cd "$TESTDIR/1.node"
echo "Sending bundle with seqId=0 (default)..."
bpdriver -1 ipn:1.2 ipn:3.1 -1000 t600 > driver_seq0.txt 2>&1

echo "Sending bundle with seqId=1..."
bpdriver -1 ipn:1.2 ipn:3.1 -1000 t600 s1 > driver_seq1.txt 2>&1

# Wait for transfers
sleep 15

# Check all custody released
cbrcustodytest -l > custody_compare.txt 2>&1

if grep -q "0 bundle(s) in custody" custody_compare.txt 2>/dev/null; then
    test_result 0 "Test 4: Both seqId=0 and seqId=1 bundles processed correctly"
else
    test_result 1 "Test 4: Custody not released for all bundles"
fi

# Cleanup receiver
kill $COUNTER_PID 2>/dev/null || true

# Show activity summary from logs
echo ""
echo "--- Activity Summary ---"
for node in 1.node 2.node 3.node; do
    if [ -f "$TESTDIR/$node/ion.log" ]; then
        cteb_count=$(grep -c "CTEB" "$TESTDIR/$node/ion.log" 2>/dev/null || echo "0")
        ccs_count=$(grep -c "CCS" "$TESTDIR/$node/ion.log" 2>/dev/null || echo "0")
        echo "  $node: $cteb_count CTEB entries, $ccs_count CCS entries"
    fi
done

# Summary
echo ""
echo "========================================="
echo "SUMMARY: $TESTS_PASSED passed, $TESTS_FAILED failed"
echo "========================================="

[ $TESTS_FAILED -eq 0 ]
