#!/usr/bin/env bash
set -euo pipefail

# nip17_stats_dm_test.sh - Test NIP-17 DM "stats" command functionality
# Sends a DM with content "stats" to the relay and checks for response

# Test key configuration (from make_and_restart_relay.sh -t)
ADMIN_PRIVATE_KEY="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
ADMIN_PUBLIC_KEY="6a04ab98d9e4774ad806e302dddeb63bea16b5cb5f223ee77478e861bb583eb3"
RELAY_PUBLIC_KEY="4f355bdcb7cc0af728ef3cceb9615d90684bb5b2ca5f859ab0f0b704075871aa"
RELAY_URL="ws://localhost:8888"

echo "=== NIP-17 DM Stats Test ==="
echo "Admin pubkey: $ADMIN_PUBLIC_KEY"
echo "Relay pubkey: $RELAY_PUBLIC_KEY"
echo "Relay URL: $RELAY_URL"
echo ""

# Check if nak is available
if ! command -v nak &> /dev/null; then
    echo "ERROR: nak command not found!"
    echo "Please install nak from https://github.com/fiatjaf/nak"
    echo "Or ensure it's in your PATH"
    exit 1
fi

echo "✓ nak command found"

# Check if relay is running by testing connection
echo "Testing relay connection..."
if ! timeout 5 nc -z localhost 8888 2>/dev/null; then
    echo "ERROR: Relay does not appear to be running on localhost:8888"
    echo "Please start the relay first with: ./make_and_restart_relay.sh"
    exit 1
fi

echo "✓ Relay appears to be running"

# Create inner DM event JSON
INNER_DM_JSON=$(cat <<EOF
{
  "kind": 14,
  "pubkey": "$ADMIN_PUBLIC_KEY",
  "created_at": $(date +%s),
  "tags": [["p", "$RELAY_PUBLIC_KEY"]],
  "content": "[\"stats\"]"
}
EOF
)

echo "Inner DM JSON:"
echo "$INNER_DM_JSON"

# Encrypt the inner DM JSON with NIP-44 using relay pubkey
echo ""
echo "Encrypting inner DM with NIP-44..."
ENCRYPTED_CONTENT=$(nak encrypt -p "$RELAY_PUBLIC_KEY" --sec "$ADMIN_PRIVATE_KEY" "$INNER_DM_JSON" 2>&1)
ENCRYPT_EXIT_CODE=$?

if [ $ENCRYPT_EXIT_CODE -ne 0 ]; then
    echo "ERROR: Failed to encrypt inner DM"
    echo "nak output: $ENCRYPTED_CONTENT"
    exit 1
fi

echo "✓ Inner DM encrypted successfully"
echo "Encrypted content: $ENCRYPTED_CONTENT"

# Send NIP-17 gift wrap event
echo ""
echo "Sending NIP-17 gift wrap with encrypted DM..."
echo "Command: nak event -k 1059 -p $RELAY_PUBLIC_KEY -c '$ENCRYPTED_CONTENT' --sec $ADMIN_PRIVATE_KEY $RELAY_URL"

DM_RESULT=$(nak event -k 1059 -p "$RELAY_PUBLIC_KEY" -c "$ENCRYPTED_CONTENT" --sec "$ADMIN_PRIVATE_KEY" "$RELAY_URL" 2>&1)
DM_EXIT_CODE=$?

if [ $DM_EXIT_CODE -ne 0 ]; then
    echo "ERROR: Failed to send gift wrap"
    echo "nak output: $DM_RESULT"
    exit 1
fi

echo "✓ Gift wrap sent successfully"
echo "nak output: $DM_RESULT"

# Wait a moment for processing
echo ""
echo "Waiting 3 seconds for relay to process and respond..."
sleep 3

# Query for gift wrap responses from the relay (kind 1059 events authored by relay)
echo ""
echo "Querying for gift wrap responses from relay..."
echo "Command: nak req -k 1059 --authors $RELAY_PUBLIC_KEY $RELAY_URL"

# Capture the output and filter for events
RESPONSE_OUTPUT=$(nak req -k 1059 --authors "$RELAY_PUBLIC_KEY" "$RELAY_URL" 2>&1)
REQ_EXIT_CODE=$?

echo ""
echo "=== Relay DM Response ==="
if [ $REQ_EXIT_CODE -eq 0 ]; then
    # Try to parse and pretty-print the JSON response
    echo "$RESPONSE_OUTPUT" | jq . 2>/dev/null || echo "$RESPONSE_OUTPUT"
else
    echo "ERROR: Failed to query DMs"
    echo "nak output: $RESPONSE_OUTPUT"
    exit 1
fi

echo ""
echo "=== Test Complete ==="
echo "If you see a gift wrap event above with encrypted content containing stats data,"
echo "then the NIP-17 DM 'stats' command is working correctly."