#!/bin/bash

# file_put_standard.sh - Simple test with known good event for debugging
# This uses a fixed event and the existing standard_test.txt file

set -e

# Configuration
SERVER_URL="http://localhost:9001"
UPLOAD_ENDPOINT="${SERVER_URL}/upload"
TEST_FILE="standard_test.txt"

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

# Known good nostr event with 1-year expiration (created with nak for standard_test.txt)
EVENT_JSON='{
  "kind": 24242,
  "id": "6f1e2ff851bde47ab2445aab8c8cbfae5c54f3d62c1892f13149fc5941ffc601",
  "pubkey": "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
  "created_at": 1756828081,
  "tags": [
    ["t", "upload"],
    ["x", "3f49f934e838893bdc516e680ade3cee2a848bbf42c3e7aba0108cf7cedb8540"],
    ["expiration", "1788364069"]
  ],
  "content": "Upload standard test file",
  "sig": "99df5bc594cfb550b3f46625d248275492ce2ec4b8f93111a192443fc6396129d9d64d3f7b380ef29c292d9fb46ca8279aeea6f91dae349ea3ca22bfb41e677f"
}'

echo "=== Ginxsom Standard Upload Test ==="
echo "Using known good event and existing standard_test.txt file"
echo

# Check if test file exists
if [ ! -f "$TEST_FILE" ]; then
    echo -e "${RED}❌ Error: $TEST_FILE not found!${NC}"
    exit 1
fi

echo -e "${BLUE}Event Details:${NC}"
echo "Pubkey: 79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
echo "Expected Hash: 3f49f934e838893bdc516e680ade3cee2a848bbf42c3e7aba0108cf7cedb8540"
echo "Event ID: 6f1e2ff851bde47ab2445aab8c8cbfae5c54f3d62c1892f13149fc5941ffc601"
echo "Expiration: 1788364069 ($(date -d @1788364069 '+%Y-%m-%d %H:%M:%S %Z'))"
echo

echo -e "${BLUE}Full Event JSON:${NC}"
echo "$EVENT_JSON" | jq '.' 2>/dev/null || echo "$EVENT_JSON"
echo

# Verify the hash matches
echo -e "${YELLOW}Hash Verification:${NC}"
CALCULATED_HASH=$(sha256sum "$TEST_FILE" | cut -d' ' -f1)
EXPECTED_HASH="3f49f934e838893bdc516e680ade3cee2a848bbf42c3e7aba0108cf7cedb8540"

echo "File: $TEST_FILE ($(wc -c < "$TEST_FILE") bytes)"
echo "Content: $(cat "$TEST_FILE")"
echo "Calculated: $CALCULATED_HASH"
echo "Expected:   $EXPECTED_HASH"

if [ "$CALCULATED_HASH" = "$EXPECTED_HASH" ]; then
    echo -e "${GREEN}✓ Hash matches!${NC}"
else
    echo -e "${RED}✗ Hash mismatch!${NC}"
    exit 1
fi
echo

# Create authorization header
echo -e "${YELLOW}Creating authorization header...${NC}"
AUTH_B64=$(echo -n "$EVENT_JSON" | base64 -w 0)
AUTH_HEADER="Nostr $AUTH_B64"
echo "Authorization header created (length: ${#AUTH_HEADER})"
echo

# Perform the upload
echo -e "${YELLOW}Uploading to ${UPLOAD_ENDPOINT}...${NC}"
echo

HTTP_STATUS=$(curl -s -w "%{http_code}" \
    -X PUT \
    -H "Authorization: $AUTH_HEADER" \
    -H "Content-Type: text/plain" \
    --data-binary @"$TEST_FILE" \
    "$UPLOAD_ENDPOINT" \
    -o response.json)

echo "HTTP Status: $HTTP_STATUS"
echo -e "${BLUE}Response:${NC}"
cat response.json | jq '.' 2>/dev/null || cat response.json
echo

# Check result
case "$HTTP_STATUS" in
    200)
        echo -e "${GREEN}✅ Upload successful!${NC}"
        ;;
    201)
        echo -e "${GREEN}✅ Upload successful (created)!${NC}"
        ;;
    400)
        echo -e "${RED}❌ Bad request${NC}"
        ;;
    401)
        echo -e "${RED}❌ Unauthorized - authentication failed${NC}"
        ;;
    *)
        echo -e "${RED}❌ Upload failed with status: $HTTP_STATUS${NC}"
        ;;
esac

# Cleanup response file only
rm -f response.json

echo
echo -e "${BLUE}Debug Info:${NC}"
echo "Check these files for detailed logs:"
echo "  - logs/fcgi-stderr.log"
echo "  - debug_validation.log" 
echo "  - logs/error.log"
