#!/bin/bash

# Test script to post kind 1 events to the relay every second
# Cycles through three different secret keys
# Content includes current timestamp
#
# Usage: ./post_events.sh <relay_url>
# Example: ./post_events.sh ws://localhost:8888
# Example: ./post_events.sh wss://relay.laantungir.net

# Check if relay URL is provided
if [ -z "$1" ]; then
    echo "Error: Relay URL is required"
    echo "Usage: $0 <relay_url>"
    echo "Example: $0 ws://localhost:8888"
    echo "Example: $0 wss://relay.laantungir.net"
    exit 1
fi

# Array of secret keys to cycle through
SECRET_KEYS=(
    "3fdd8227a920c2385559400b2b14e464f22e80df312a73cc7a86e1d7e91d608f"
    "a156011cd65b71f84b4a488ac81687f2aed57e490b31c28f58195d787030db60"
    "1618aaa21f5bd45c5ffede0d9a60556db67d4a046900e5f66b0bae5c01c801fb"
)

RELAY_URL="$1"
KEY_INDEX=0

echo "Starting event posting test to $RELAY_URL"
echo "Press Ctrl+C to stop"

while true; do
    # Get current timestamp
    TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S UTC")

    # Get current secret key
    CURRENT_KEY=${SECRET_KEYS[$KEY_INDEX]}

    # Create content with timestamp
    CONTENT="Test event at $TIMESTAMP"

    echo "[$TIMESTAMP] Posting event with key ${KEY_INDEX}: ${CURRENT_KEY:0:16}..."

    # Post event using nak
    nak event -c "$CONTENT" --sec "$CURRENT_KEY" "$RELAY_URL"

    # Cycle to next key
    KEY_INDEX=$(( (KEY_INDEX + 1) % ${#SECRET_KEYS[@]} ))

    # Wait 1 second
    sleep .2
done