#!/bin/bash

# Test walrus relay CLI commands (status, enable, disable)
# Tests the bash-level functionality without requiring Rust daemon support

# Ignore SIGPIPE on macOS to prevent test failures
if [[ "$(uname)" == "Darwin" ]]; then
    trap '' SIGPIPE
    echo "SIGPIPE trap installed"
fi

# Load common test functions
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=SCRIPTDIR/__test_common.sh
source "$script_dir/__test_common.sh"


# Test plan
echo "=== Testing Walrus Relay CLI Commands ==="
echo "Testing: $WORKDIR wal-relay status/enable/disable"
echo

# Setup test environment
setup_test_workdir "$WORKDIR"
backup_config_files "$WORKDIR"

# Ensure we're using BUILD version of suibase-daemon for walrus relay features
ensure_build_daemon

# Store original config state
ORIGINAL_CONFIG_STATE=""
if grep -q "^walrus_relay_enabled:" "$WORKDIRS/$WORKDIR/suibase.yaml" 2>/dev/null; then
    ORIGINAL_CONFIG_STATE=$(grep "^walrus_relay_enabled:" "$WORKDIRS/$WORKDIR/suibase.yaml")
fi

test_status_when_disabled() {
    echo "--- Test: Status when disabled ---"

    # Ensure disabled state  
    "$SUIBASE_DIR/scripts/$WORKDIR" wal-relay disable
    
    # Start services so CLI reads config state instead of showing STOPPED
    "$SUIBASE_DIR/scripts/$WORKDIR" start

    # Test status output - CLI detects DISABLED instantaneously from config
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay status 2>&1)
    echo "$output"

    if ! echo "$output" | grep -q "DISABLED"; then
        fail "Status should show DISABLED when relay is disabled. Got: $output"
    fi

    if ! echo "$output" | grep -q "To enable do '$WORKDIR wal-relay enable'"; then
        fail "Status should show enable instruction when disabled. Got: $output"
    fi

    echo "✓ Status correctly shows DISABLED state with enable instruction"
}

test_enable_command() {
    echo "--- Test: Enable command ---"

    # Ensure starting from disabled state
    "$SUIBASE_DIR/scripts/$WORKDIR" wal-relay disable

    # Test enable command (should change from disabled to enabled)
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay enable 2>&1)
    echo "$output"

    # The enable command should report that it enabled walrus relay
    if ! echo "$output" | grep -q "Walrus relay is now enabled"; then
        fail "Enable command should confirm enablement. Got: $output"
    fi

    # Verify config file was updated
    if ! grep -q "^walrus_relay_enabled: true" "$WORKDIRS/$WORKDIR/suibase.yaml"; then
        fail "Config file should contain 'walrus_relay_enabled: true' after enable"
    fi

    echo "✓ Enable command works and updates config file"
}

test_status_when_enabled() {
    echo "--- Test: Status when enabled (daemon supports walrus relay) ---"

    # NOTE: With daemon walrus support implemented, this tests the real behavior:
    # When walrus_relay_enabled=true and daemon supports walrus relay,
    # status shows DOWN when process is not running, or OK when process is running

    # Ensure enabled state but stop services so process isn't running
    "$SUIBASE_DIR/scripts/$WORKDIR" wal-relay enable
    "$SUIBASE_DIR/scripts/$WORKDIR" stop

    # Test status output
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay status 2>&1)
    echo "$output"

    # Should show DOWN, STOPPED, or OK depending on daemon and process state
    if echo "$output" | grep -q "DOWN"; then
        echo "✓ Status correctly shows DOWN state when enabled but process not running"
    elif echo "$output" | grep -q "STOPPED"; then
        echo "✓ Status correctly shows STOPPED state when services are stopped"
    elif echo "$output" | grep -q "OK"; then
        echo "✓ Status correctly shows OK state when enabled and process running"
    else
        fail "Status should show DOWN, STOPPED, or OK when enabled with daemon support. Got: $output"
    fi
}

test_status_when_working() {
    echo "--- Test: Status when working (daemon integration) ---"

    # NOTE: With daemon walrus support implemented, this tests actual OK/DOWN states
    # When enabled and services running, status should be OK or DOWN from daemon

    # Ensure enabled state and start services  
    "$SUIBASE_DIR/scripts/$WORKDIR" wal-relay enable
    "$SUIBASE_DIR/scripts/$WORKDIR" start

    # Test status output 
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay status 2>&1)

    # Wait for INITIALIZING to resolve to OK or DOWN (max 10 seconds)
    if ! wait_for_walrus_relay_status "$WORKDIR" "OK|DOWN" 10 true; then
        echo "⚠ Walrus relay status did not resolve from INITIALIZING within 10 seconds"
    fi
    
    # Get final status
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay status 2>&1)
    
    # Check final status after INITIALIZING resolved (or timeout)
    if echo "$output" | grep -q "OK"; then
        # Daemon implementation working - process running and healthy
        # Check for proxy URL based on workdir
        local expected_port
        case "$WORKDIR" in
            "testnet") expected_port="45852" ;;
            "mainnet") expected_port="45853" ;;
            *) expected_port="45852" ;; # fallback
        esac
        # Strip ANSI color codes before checking for URL pattern
        if ! strip_ansi_colors "$output" | grep -q "http://localhost:$expected_port"; then
            fail "Status should show proxy URL http://localhost:$expected_port when OK. Got: $output"
        fi
        # Wait for PID to show up (handle race condition where process is OK but PID not yet detected)
        local pid_found=false
        local pid_retry_count=0
        local max_pid_retries=10
        
        while [ $pid_retry_count -lt $max_pid_retries ]; do
            if echo "$output" | grep -q "pid"; then
                pid_found=true
                break
            fi
            echo "⚠ PID not found in status output, retrying ($((pid_retry_count + 1))/$max_pid_retries)..."
            sleep 1
            output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay status 2>&1)
            pid_retry_count=$((pid_retry_count + 1))
        done
        
        if [ "$pid_found" = false ]; then
            fail "Status should show PID when OK and running after $max_pid_retries retries. Got: $output"  
        fi
        # Extract PID from output for display (format is "( pid 12345 )")
        local displayed_pid
        # First strip ANSI color codes, then extract PID
        displayed_pid=$(strip_ansi_colors "$output" | grep -o "( pid [0-9]* )" | grep -o "[0-9]*")
        echo "✓ Status correctly shows OK state with proxy URL and PID ($displayed_pid)"
    elif echo "$output" | grep -q "DOWN"; then
        # Daemon detects process not running or unhealthy
        echo "✓ Status correctly shows DOWN state (daemon detects process issue)"
    elif echo "$output" | grep -q "INITIALIZING"; then
        # Timeout waiting for INITIALIZING to resolve
        fail "Status stuck in INITIALIZING after 10 seconds. Got: $output"
    elif echo "$output" | grep -q "NOT RUNNING"; then
        # Daemon not running (should not happen if testnet start worked)
        echo "⚠ Status shows NOT RUNNING - daemon may not be running"
    else
        fail "Status should show OK or DOWN when enabled with daemon support. Got: $output"
    fi
}

test_disable_command() {
    echo "--- Test: Disable command ---"

    # Ensure starting from enabled state
    "$SUIBASE_DIR/scripts/$WORKDIR" wal-relay enable

    # Test disable command
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay disable 2>&1)
    echo "$output"

    if ! echo "$output" | grep -q "Walrus relay is now disabled"; then
        fail "Disable command should confirm disablement. Got: $output"
    fi

    # Verify config file was updated
    if ! grep -q "^walrus_relay_enabled: false" "$WORKDIRS/$WORKDIR/suibase.yaml"; then
        fail "Config file should contain 'walrus_relay_enabled: false' after disable"
    fi

    echo "✓ Disable command works and updates config file"
}

test_enable_when_already_enabled() {
    echo "--- Test: Enable when already enabled ---"

    # Ensure enabled state
    "$SUIBASE_DIR/scripts/$WORKDIR" wal-relay enable

    # Test enable command again
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay enable 2>&1)
    echo "$output"

    if ! echo "$output" | grep -q "Walrus relay already enabled"; then
        fail "Enable command should detect already enabled state. Got: $output"
    fi

    echo "✓ Enable command correctly detects already enabled state"
}

test_disable_when_already_disabled() {
    echo "--- Test: Disable when already disabled ---"

    # Ensure disabled state
    "$SUIBASE_DIR/scripts/$WORKDIR" wal-relay disable

    # Test disable command again
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay disable 2>&1)
    echo "$output"

    if ! echo "$output" | grep -q "Walrus relay already disabled"; then
        fail "Disable command should detect already disabled state. Got: $output"
    fi

    echo "✓ Disable command correctly detects already disabled state"
}

test_mainnet_support() {
    echo "--- Test: Mainnet support ---"

    # Test mainnet status (should work)
    local output
    output=$("$SUIBASE_DIR/scripts/mainnet" wal-relay status 2>&1)
    echo "$output"

    # Strip ANSI color codes and check for valid status (DISABLED, DOWN, OK, etc.)
    if ! strip_ansi_colors "$output" | grep -Eq "DISABLED|DOWN|OK|STOPPED|NOT RUNNING"; then
        fail "Mainnet wal-relay status should work. Got: $output"
    fi

    echo "✓ Mainnet wal-relay commands work"
}

test_unsupported_network() {
    echo "--- Test: Unsupported network ---"

    # Test devnet (should fail)
    local output
    output=$("$SUIBASE_DIR/scripts/devnet" wal-relay status 2>&1 || true)

    if ! echo "$output" | grep -q "not supported for devnet"; then
        fail "Devnet wal-relay should show error. Got: $output"
    fi

    echo "✓ Unsupported networks properly reject wal-relay commands"
}

test_help_command() {
    echo "--- Test: Help command ---"

    # Test help output
    local output
    output=$("$SUIBASE_DIR/scripts/$WORKDIR" wal-relay --help 2>&1)

    if ! echo "$output" | grep -q "USAGE:"; then
        fail "Help should show usage information. Got: $output"
    fi

    if ! echo "$output" | grep -q "status" || ! echo "$output" | grep -q "enable" || ! echo "$output" | grep -q "disable"; then
        fail "Help should show available subcommands. Got: $output"
    fi

    if ! echo "$output" | grep -q "Application → suibase-daemon"; then
        fail "Help should show architecture information. Got: $output"
    fi

    echo "✓ Help command shows proper usage information"
}

# Run tests
test_status_when_disabled
test_enable_command
test_status_when_enabled
test_status_when_working
test_disable_command
test_enable_when_already_enabled
test_disable_when_already_disabled
test_mainnet_support
test_unsupported_network
test_help_command

# Restore original config state
echo "--- Restoring original configuration ---"

# Check if suibase.yaml file exists before attempting to modify it
if [ ! -f "$WORKDIRS/$WORKDIR/suibase.yaml" ]; then
    echo "ERROR: suibase.yaml file not found at $WORKDIRS/$WORKDIR/suibase.yaml"
    exit 1
fi

if [ -n "$ORIGINAL_CONFIG_STATE" ]; then
    # Remove any existing walrus_relay_enabled line and add the original
    sed -i.bak '/^walrus_relay_enabled:/d' "$WORKDIRS/$WORKDIR/suibase.yaml" && rm "$WORKDIRS/$WORKDIR/suibase.yaml.bak"
    echo "$ORIGINAL_CONFIG_STATE" >> "$WORKDIRS/$WORKDIR/suibase.yaml"
    echo "✓ Restored original config: $ORIGINAL_CONFIG_STATE"
else
    # Remove walrus_relay_enabled line if it didn't exist originally
    sed -i.bak '/^walrus_relay_enabled:/d' "$WORKDIRS/$WORKDIR/suibase.yaml" && rm "$WORKDIRS/$WORKDIR/suibase.yaml.bak"
    echo "✓ Removed walrus_relay_enabled (wasn't present originally)"
fi

echo
echo "=== All Walrus Relay CLI Tests Passed! ==="
echo