#!/bin/bash

# upload_html_test.sh - Test script for uploading HTML to production Ginxsom Blossom server
# Tests upload functionality on blossom.laantungir.net

set -e  # Exit on any error

# Configuration
SERVER_URL="https://blossom.laantungir.net"
UPLOAD_ENDPOINT="${SERVER_URL}/upload"
TEST_FILE="tests/test_upload.html"

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

# Helper functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

# Calculate file hash
calculate_hash() {
    log_info "Calculating SHA-256 hash..."
    HASH=$(sha256sum "${TEST_FILE}" | cut -d' ' -f1)
    log_success "File hash: ${HASH}"
}

# Generate nostr event
generate_nostr_event() {
    log_info "Generating kind 24242 nostr event..."
    EXPIRATION=$(date -d '+1 hour' +%s)
    EVENT_JSON=$(nak event -k 24242 -c "" \
        --sec "22cc83aa57928a2800234c939240c9a6f0f44a33ea3838a860ed38930b195afd" \
        -t "t=upload" \
        -t "x=${HASH}" \
        -t "expiration=${EXPIRATION}")

    if [[ -z "$EVENT_JSON" ]]; then
        log_error "Failed to generate nostr event"
        exit 1
    fi
    log_success "Generated nostr event"
}

# Create authorization header
create_auth_header() {
    log_info "Creating authorization header..."
    AUTH_B64=$(echo -n "$EVENT_JSON" | base64 -w 0)
    AUTH_HEADER="Nostr ${AUTH_B64}"
    log_success "Created authorization header"
}

# Perform upload
perform_upload() {
    log_info "Performing upload to ${UPLOAD_ENDPOINT}..."
    RESPONSE_FILE=$(mktemp)
    
    HTTP_STATUS=$(curl -s -w "%{http_code}" \
        -X PUT \
        -H "Authorization: ${AUTH_HEADER}" \
        -H "Content-Type: text/html" \
        --data-binary "@${TEST_FILE}" \
        "${UPLOAD_ENDPOINT}" \
        -o "${RESPONSE_FILE}")
    
    echo "HTTP Status: ${HTTP_STATUS}"
    echo "Response body:"
    cat "${RESPONSE_FILE}"
    echo
    rm -f "${RESPONSE_FILE}"
    
    if [[ "${HTTP_STATUS}" == "200" || "${HTTP_STATUS}" == "201" ]]; then
        log_success "Upload successful!"
    else
        log_error "Upload failed with HTTP status: ${HTTP_STATUS}"
        exit 1
    fi
}

# Test file retrieval
test_retrieval() {
    log_info "Testing file retrieval..."
    
    # Try with .html extension
    RETRIEVAL_URL_EXT="${SERVER_URL}/${HASH}.html"
    log_info "Trying retrieval with extension: ${RETRIEVAL_URL_EXT}"
    if curl -s -f -I "${RETRIEVAL_URL_EXT}" > /dev/null 2>&1; then
        log_success "File found with .html extension!"
    else
        log_warning "File NOT found with .html extension"
    fi

    # Try without extension
    RETRIEVAL_URL="${SERVER_URL}/${HASH}"
    log_info "Trying retrieval without extension: ${RETRIEVAL_URL}"
    if curl -s -f -I "${RETRIEVAL_URL}" > /dev/null 2>&1; then
        log_success "File found without extension!"
    else
        log_warning "File NOT found without extension"
    fi
}

# Main execution
main() {
    calculate_hash
    generate_nostr_event
    create_auth_header
    perform_upload
    test_retrieval
}

main "$@"
