#!/bin/bash

# Mirror Test Script for BUD-04
# Tests the PUT /mirror endpoint with a sample PNG file and NIP-42 authentication

# ============================================================================
# CONFIGURATION - Choose your target Blossom server
# ============================================================================

# Local server (uncomment to use)
BLOSSOM_SERVER="http://localhost:9001"

# Remote server (uncomment to use)
#BLOSSOM_SERVER="https://blossom.laantungir.net"

# ============================================================================

# Test URL - PNG file with known SHA-256 hash
TEST_URL="https://laantungir.github.io/img_repo/24308d48eb498b593e55a87b6300ccffdea8432babc0bb898b1eff21ebbb72de.png"
EXPECTED_HASH="24308d48eb498b593e55a87b6300ccffdea8432babc0bb898b1eff21ebbb72de"

echo "=== BUD-04 Mirror Endpoint Test with Authentication ==="
echo "Blossom Server: $BLOSSOM_SERVER"
echo "Target URL: $TEST_URL"
echo "Expected Hash: $EXPECTED_HASH"
echo ""

# Get a fresh challenge from the server
echo "=== Getting Authentication Challenge ==="
challenge=$(curl -s "$BLOSSOM_SERVER/auth" | jq -r '.challenge')
if [ "$challenge" = "null" ] || [ -z "$challenge" ]; then
    echo "❌ Failed to get challenge from server"
    exit 1
fi
echo "Challenge: $challenge"

# Create NIP-42 auth event (kind 22242) with challenge using hex private key
TEST_USER_PRIVKEY="0000000000000000000000000000000000000000000000000000000000000001"
expiration=$(date -d "+3600 seconds" +%s)
event=$(nak event -k 22242 --tag "relay=ginxsom" --tag "challenge=$challenge" --tag "expiration=$expiration" --sec "$TEST_USER_PRIVKEY")
auth_header="Nostr $(echo "$event" | base64 -w 0)"
echo "Created NIP-42 auth event"
echo ""

# Create JSON request body
JSON_BODY=$(cat <<EOF
{
  "url": "$TEST_URL"
}
EOF
)

echo "Request Body:"
echo "$JSON_BODY"
echo ""

# Make the mirror request with authentication
echo "=== Making Authenticated Mirror Request ==="
RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}\n" \
  -X PUT \
  -H "Authorization: $auth_header" \
  -H "Content-Type: application/json" \
  -d "$JSON_BODY" \
  "$BLOSSOM_SERVER/mirror")

echo "Response:"
echo "$RESPONSE"
echo ""

# Extract HTTP status code
HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
echo "HTTP Status Code: $HTTP_CODE"

# Check if successful
if [ "$HTTP_CODE" = "200" ]; then
    echo "✅ Mirror request successful!"
    
    # Try to access the mirrored blob
    echo ""
    echo "=== Verifying Mirrored Blob ==="
    echo "Attempting to fetch: $BLOSSOM_SERVER/$EXPECTED_HASH.png"
    
    BLOB_RESPONSE=$(curl -s -w "HTTP_CODE:%{http_code}" -I "$BLOSSOM_SERVER/$EXPECTED_HASH.png")
    BLOB_HTTP_CODE=$(echo "$BLOB_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
    
    if [ "$BLOB_HTTP_CODE" = "200" ]; then
        echo "✅ Mirrored blob accessible!"
        echo ""
        echo "=== Blob Headers ==="
        echo "$BLOB_RESPONSE" | grep -v "HTTP_CODE:"
    else
        echo "❌ Mirrored blob not accessible (HTTP $BLOB_HTTP_CODE)"
    fi
    
    # Test HEAD request for metadata
    echo ""
    echo "=== Testing HEAD Request ==="
    HEAD_RESPONSE=$(curl -s -w "HTTP_CODE:%{http_code}" -I -X HEAD "$BLOSSOM_SERVER/$EXPECTED_HASH")
    HEAD_HTTP_CODE=$(echo "$HEAD_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
    
    if [ "$HEAD_HTTP_CODE" = "200" ]; then
        echo "✅ HEAD request successful!"
        echo "Metadata headers:"
        echo "$HEAD_RESPONSE" | grep -E "(Content-Type|Content-Length|ETag)" | grep -v "HTTP_CODE:"
    else
        echo "❌ HEAD request failed (HTTP $HEAD_HTTP_CODE)"
    fi
    
else
    echo "❌ Mirror request failed (HTTP $HTTP_CODE)"
    if [ "$HTTP_CODE" != "000" ]; then
        echo "Response body:"
        echo "$RESPONSE" | grep -v "HTTP_CODE:"
    fi
fi

echo ""
echo "=== Test Complete ==="