#!/bin/bash

# E2E Test: Large File Upload Flow
# Tests torrent storage path for files over 100MB

set -e

BASE_URL="http://localhost:9876"
TEST_FILE="/tmp/large_test_file.bin"

echo "=== Large File Upload E2E Test ==="

# Create test file (150MB)
echo "Creating 150MB test file..."
dd if=/dev/urandom of="$TEST_FILE" bs=1048576 count=150 2>/dev/null
echo "Created test file: $(ls -lh $TEST_FILE)"

# Test 1: Upload large file (requires authentication)
echo "Uploading large file..."
# Note: This test requires a running gateway with a test session in the database
TEST_PUBKEY="1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
SESSION_TOKEN="test_session_token_${TEST_PUBKEY}"

UPLOAD_START=$(date +%s)

UPLOAD_RESPONSE=$(curl -s -X POST \
    -H "Authorization: Bearer $SESSION_TOKEN" \
    -F "file=@$TEST_FILE" \
    "$BASE_URL/api/upload" \
    --max-time 300)  # 5 minute timeout

UPLOAD_END=$(date +%s)
UPLOAD_TIME=$((UPLOAD_END - UPLOAD_START))

echo "Upload completed in ${UPLOAD_TIME} seconds"
echo "Upload response: $UPLOAD_RESPONSE"

# Extract file hash from response
FILE_HASH=$(echo "$UPLOAD_RESPONSE" | grep -o '"file_hash":"[^"]*"' | cut -d'"' -f4)
MESSAGE=$(echo "$UPLOAD_RESPONSE" | grep -o '"message":"[^"]*"' | cut -d'"' -f4)

if [ -z "$FILE_HASH" ]; then
    echo "❌ Failed to get file hash from upload response"
    exit 1
fi

echo "✅ Large file uploaded successfully"
echo "   File hash: $FILE_HASH"
echo "   Message: $MESSAGE"
echo "   Upload time: ${UPLOAD_TIME}s"

# Verify storage type is torrent for large file (check message)
if ! echo "$MESSAGE" | grep -q "as torrent"; then
    echo "❌ Expected 'as torrent' in message but got '$MESSAGE'"
    exit 1
fi
echo "✅ Correct storage type (torrent) for large file"

# Test 2: Get torrent file
echo "Getting torrent file..."
TORRENT_RESPONSE=$(curl -s "$BASE_URL/api/torrent/$FILE_HASH")

if [ -z "$TORRENT_RESPONSE" ]; then
    echo "❌ Failed to get torrent file"
    exit 1
fi
echo "✅ Torrent file generated successfully"

# Test 3: Download large file
echo "Downloading large file..."
DOWNLOAD_FILE="/tmp/downloaded_large_file.bin"
DOWNLOAD_START=$(date +%s)

curl -s -H "User-Agent: TestRunner/1.0" "$BASE_URL/api/download/$FILE_HASH" -o "$DOWNLOAD_FILE" --max-time 300

DOWNLOAD_END=$(date +%s)
DOWNLOAD_TIME=$((DOWNLOAD_END - DOWNLOAD_START))

if [ ! -f "$DOWNLOAD_FILE" ]; then
    echo "❌ Download failed - file not created"
    exit 1
fi

echo "Download completed in ${DOWNLOAD_TIME} seconds"

# Verify file integrity
echo "Verifying file integrity..."
ORIGINAL_HASH=$(sha256sum "$TEST_FILE" | cut -d' ' -f1)
DOWNLOADED_HASH=$(sha256sum "$DOWNLOAD_FILE" | cut -d' ' -f1)

if [ "$ORIGINAL_HASH" != "$DOWNLOADED_HASH" ]; then
    echo "❌ File integrity check failed"
    echo "   Original:   $ORIGINAL_HASH"
    echo "   Downloaded: $DOWNLOADED_HASH"
    exit 1
fi
echo "✅ Large file downloaded successfully with correct content"

# Test 4: Check chunk creation
echo "Verifying chunk storage..."
STATS_RESPONSE=$(curl -s "$BASE_URL/api/stats")
CHUNK_COUNT=$(echo "$STATS_RESPONSE" | grep -o '"chunks":[0-9]*' | cut -d':' -f2)

if [ "$CHUNK_COUNT" -eq "0" ]; then
    echo "❌ Expected chunks to be created but got $CHUNK_COUNT"
    exit 1
fi
echo "✅ File properly chunked - $CHUNK_COUNT chunks created"

# Test 5: Performance metrics
echo "Performance metrics:"
echo "   File size: 150MB"
echo "   Upload time: ${UPLOAD_TIME}s ($(echo "scale=2; 150 / $UPLOAD_TIME" | bc -l) MB/s)"
echo "   Download time: ${DOWNLOAD_TIME}s ($(echo "scale=2; 150 / $DOWNLOAD_TIME" | bc -l) MB/s)"
echo "   Chunks created: $CHUNK_COUNT"

# Cleanup
rm -f "$TEST_FILE" "$DOWNLOAD_FILE"

echo ""
echo "🎉 All large file upload tests passed!"
echo "✅ Upload -> Torrent Storage -> Chunking -> Download cycle working"
echo "✅ File integrity preserved through chunking"
echo "✅ Performance within acceptable range"