#!/bin/bash

# NIP Protocol Test Runner for C-Relay
# Runs all NIP compliance tests

set -e

# Configuration
RELAY_HOST="127.0.0.1"
RELAY_PORT="8888"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Test counters
TOTAL_SUITES=0
PASSED_SUITES=0
FAILED_SUITES=0

# Available NIP test files
NIP_TESTS=(
    "1_nip_test.sh:NIP-01 Basic Protocol"
    "9_nip_delete_test.sh:NIP-09 Event Deletion"
    "11_nip_information.sh:NIP-11 Relay Information"
    "13_nip_test.sh:NIP-13 Proof of Work"
    "17_nip_test.sh:NIP-17 Private DMs"
    "40_nip_test.sh:NIP-40 Expiration Timestamp"
    "42_nip_test.sh:NIP-42 Authentication"
    "45_nip_test.sh:NIP-45 Event Counts"
    "50_nip_test.sh:NIP-50 Search Capability"
    "70_nip_test.sh:NIP-70 Protected Events"
)

# Function to run a NIP test suite
run_nip_test() {
    local test_file="$1"
    local test_name="$2"

    TOTAL_SUITES=$((TOTAL_SUITES + 1))

    echo "=========================================="
    echo "Running $test_name ($test_file)"
    echo "=========================================="

    if [[ ! -f "$test_file" ]]; then
        echo -e "${RED}ERROR: Test file $test_file not found${NC}"
        FAILED_SUITES=$((FAILED_SUITES + 1))
        return 1
    fi

    # Make script executable if not already
    chmod +x "$test_file"

    # Run the test
    if bash "$test_file"; then
        echo -e "${GREEN}✓ $test_name PASSED${NC}"
        PASSED_SUITES=$((PASSED_SUITES + 1))
        return 0
    else
        echo -e "${RED}✗ $test_name FAILED${NC}"
        FAILED_SUITES=$((FAILED_SUITES + 1))
        return 1
    fi
}

# Function to check relay connectivity
check_relay() {
    echo "Checking relay connectivity at ws://$RELAY_HOST:$RELAY_PORT..."

    if timeout 5 bash -c "
        echo 'ping' | websocat -n1 ws://$RELAY_HOST:$RELAY_PORT >/dev/null 2>&1
    " 2>/dev/null; then
        echo -e "${GREEN}✓ Relay is accessible${NC}"
        return 0
    else
        echo -e "${RED}✗ Cannot connect to relay${NC}"
        echo "Please start the relay first: ./make_and_restart_relay.sh"
        return 1
    fi
}

echo "=========================================="
echo "C-Relay NIP Protocol Test Suite"
echo "=========================================="
echo "Testing NIP compliance against relay at ws://$RELAY_HOST:$RELAY_PORT"
echo ""

# Check relay connectivity
if ! check_relay; then
    exit 1
fi

echo ""
echo "Running NIP protocol tests..."
echo ""

# Run all NIP tests
for nip_test in "${NIP_TESTS[@]}"; do
    test_file="${nip_test%%:*}"
    test_name="${nip_test#*:}"

    run_nip_test "$test_file" "$test_name"
    echo ""
done

# Summary
echo "=========================================="
echo "NIP Test Summary"
echo "=========================================="
echo "Total NIP test suites: $TOTAL_SUITES"
echo -e "Passed: ${GREEN}$PASSED_SUITES${NC}"
echo -e "Failed: ${RED}$FAILED_SUITES${NC}"

if [[ $FAILED_SUITES -eq 0 ]]; then
    echo -e "${GREEN}✓ All NIP tests passed!${NC}"
    echo "The relay is fully NIP compliant."
    exit 0
else
    echo -e "${RED}✗ Some NIP tests failed.${NC}"
    echo "The relay may have NIP compliance issues."
    exit 1
fi