#!/bin/bash

# list_test.sh - Test script for GET /list/<pubkey> endpoint
# This script tests the blob listing functionality

BASE_URL="http://localhost:9001"
NOSTR_PRIVKEY="22cc83aa57928a2800234c939240c9a6f0f44a33ea3838a860ed38930b195afd"
NOSTR_PUBKEY="8ff74724ed641b3c28e5a86d7c5cbc49c37638ace8c6c38935860e7a5eedde0e"

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

echo "=== Ginxsom List Blobs Tests ==="
echo

# Function to generate a Nostr event for list authorization
generate_list_auth() {
    local content="$1"

    # Use nak to generate properly signed events with Alice's private key
    nak event -k 24242 -c "$content" \
        --sec "$NOSTR_PRIVKEY" \
        -t "t=list" \
        -t "expiration=$(( $(date +%s) + 3600 ))"
}

# Test 1: List blobs without authorization (should work if optional auth)
echo -e "${YELLOW}Test 1: GET /list/<pubkey> without authorization${NC}"

RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" "$BASE_URL/list/$NOSTR_PUBKEY")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

echo "Using pubkey: $NOSTR_PUBKEY"
echo "HTTP Status: $HTTP_STATUS"
echo "Response: $BODY"
echo

# Test 2: List blobs with authorization
echo -e "${YELLOW}Test 2: GET /list/<pubkey> with authorization${NC}"
LIST_AUTH=$(generate_list_auth "List Blobs")
AUTH_B64=$(echo "$LIST_AUTH" | base64 -w 0)
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
    -H "Authorization: Nostr $AUTH_B64" \
    "$BASE_URL/list/$NOSTR_PUBKEY")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

echo "HTTP Status: $HTTP_STATUS"
echo "Response: $BODY"
echo

# Test 3: List blobs with since parameter
echo -e "${YELLOW}Test 3: GET /list/<pubkey> with since parameter${NC}"
SINCE_TIMESTAMP=$(($(date +%s) - 86400))  # 24 hours ago
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
    "$BASE_URL/list/$NOSTR_PUBKEY?since=$SINCE_TIMESTAMP")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

echo "HTTP Status: $HTTP_STATUS"
echo "Response: $BODY"
echo

# Test 4: List blobs with until parameter
echo -e "${YELLOW}Test 4: GET /list/<pubkey> with until parameter${NC}"
UNTIL_TIMESTAMP=$(date +%s)  # now
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
    "$BASE_URL/list/$NOSTR_PUBKEY?until=$UNTIL_TIMESTAMP")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

echo "HTTP Status: $HTTP_STATUS"
echo "Response: $BODY"
echo

# Test 5: List blobs with both since and until parameters
echo -e "${YELLOW}Test 5: GET /list/<pubkey> with since and until parameters${NC}"
SINCE_TIMESTAMP=$(($(date +%s) - 86400))  # 24 hours ago
UNTIL_TIMESTAMP=$(date +%s)  # now
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
    "$BASE_URL/list/$NOSTR_PUBKEY?since=$SINCE_TIMESTAMP&until=$UNTIL_TIMESTAMP")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

echo "HTTP Status: $HTTP_STATUS"
echo "Response: $BODY"
echo

# Test 6: List blobs for non-existent pubkey
echo -e "${YELLOW}Test 6: GET /list/<nonexistent_pubkey>${NC}"
FAKE_PUBKEY="1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" "$BASE_URL/list/$FAKE_PUBKEY")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

if [ "$HTTP_STATUS" = "200" ]; then
    echo -e "${GREEN}✓ Correctly returned 200 with empty array${NC}"
else
    echo "HTTP Status: $HTTP_STATUS"
fi
echo "Response: $BODY"
echo

# Test 7: List blobs with invalid pubkey format
echo -e "${YELLOW}Test 7: GET /list/<invalid_pubkey_format>${NC}"
INVALID_PUBKEY="invalid_pubkey"
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" "$BASE_URL/list/$INVALID_PUBKEY")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

if [ "$HTTP_STATUS" = "400" ]; then
    echo -e "${GREEN}✓ Correctly returned 400 for invalid pubkey format${NC}"
else
    echo "HTTP Status: $HTTP_STATUS"
fi
echo "Response: $BODY"
echo

# Test 8: List blobs with invalid since/until parameters
echo -e "${YELLOW}Test 8: GET /list/<pubkey> with invalid timestamp parameters${NC}"
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \
    "$BASE_URL/list/$NOSTR_PUBKEY?since=invalid&until=invalid")
HTTP_STATUS=$(echo "$RESPONSE" | grep "HTTP_STATUS" | cut -d: -f2)
BODY=$(echo "$RESPONSE" | sed '/HTTP_STATUS/d')

echo "HTTP Status: $HTTP_STATUS"
echo "Response: $BODY"
echo

# echo "=== List Tests Complete ==="
# echo
# echo "Expected blob descriptor format:"
# echo '{'
# echo '  "url": "https://server.com/<sha256>.<ext>",'
# echo '  "sha256": "<sha256>",'
# echo '  "size": <bytes>,'
# echo '  "type": "<mime_type>",'
# echo '  "uploaded": <unix_timestamp>'
# echo '}'
# echo
# echo "Note: These tests use placeholder Nostr events."
# echo "For real testing, use proper Nostr signing tools to generate valid events."
