#!/bin/bash
# Download All Test Data
#
# Downloads and organizes all test data needed for blvm-consensus testing:
# - Bitcoin Core test vectors (tx_valid.json, tx_invalid.json)
# - Mainnet blocks at key consensus-era heights
# - Historical block checkpoints (if available)
#
# Usage:
#   ./scripts/download_test_data.sh [--all] [--core-vectors] [--mainnet-blocks] [--checkpoints]
#
# Options:
#   --all              Download all test data
#   --core-vectors     Download Core test vectors only
#   --mainnet-blocks   Download mainnet blocks only
#   --checkpoints      Download checkpoint data only

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
TEST_DATA_DIR="$PROJECT_ROOT/tests/test_data"

# Create test data directory structure
mkdir -p "$TEST_DATA_DIR/core_vectors/transactions"
mkdir -p "$TEST_DATA_DIR/core_vectors/blocks"
mkdir -p "$TEST_DATA_DIR/mainnet_blocks"
mkdir -p "$TEST_DATA_DIR/checkpoints"

# Parse arguments
DOWNLOAD_ALL=false
DOWNLOAD_CORE=false
DOWNLOAD_MAINNET=false
DOWNLOAD_CHECKPOINTS=false

if [ $# -eq 0 ]; then
    DOWNLOAD_ALL=true
fi

for arg in "$@"; do
    case $arg in
        --all)
            DOWNLOAD_ALL=true
            ;;
        --core-vectors)
            DOWNLOAD_CORE=true
            ;;
        --mainnet-blocks)
            DOWNLOAD_MAINNET=true
            ;;
        --checkpoints)
            DOWNLOAD_CHECKPOINTS=true
            ;;
        *)
            echo "Unknown option: $arg"
            echo "Usage: $0 [--all] [--core-vectors] [--mainnet-blocks] [--checkpoints]"
            exit 1
            ;;
    esac
done

# Download Core test vectors
if [ "$DOWNLOAD_ALL" = true ] || [ "$DOWNLOAD_CORE" = true ]; then
    echo "=== Downloading Core Test Vectors ==="
    
    CORE_VECTORS_DIR="$TEST_DATA_DIR/core_vectors/transactions"
    
    # Download transaction test vectors
    if [ ! -f "$CORE_VECTORS_DIR/tx_valid.json" ]; then
        echo "Downloading tx_valid.json..."
        curl -L -o "$CORE_VECTORS_DIR/tx_valid.json" \
            "https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/tx_valid.json" || {
            echo "Warning: Failed to download tx_valid.json from GitHub"
            echo "  Alternative: Download manually from Bitcoin Core repository"
        }
    else
        echo "✅ tx_valid.json already exists"
    fi
    
    if [ ! -f "$CORE_VECTORS_DIR/tx_invalid.json" ]; then
        echo "Downloading tx_invalid.json..."
        curl -L -o "$CORE_VECTORS_DIR/tx_invalid.json" \
            "https://raw.githubusercontent.com/bitcoin/bitcoin/master/src/test/data/tx_invalid.json" || {
            echo "Warning: Failed to download tx_invalid.json from GitHub"
            echo "  Alternative: Download manually from Bitcoin Core repository"
        }
    else
        echo "✅ tx_invalid.json already exists"
    fi
    
    echo ""
fi

# Download mainnet blocks
if [ "$DOWNLOAD_ALL" = true ] || [ "$DOWNLOAD_MAINNET" = true ]; then
    echo "=== Downloading Mainnet Blocks ==="
    
    # Use the mainnet blocks download script if it exists
    if [ -f "$SCRIPT_DIR/download_mainnet_blocks.sh" ]; then
        echo "Using download_mainnet_blocks.sh..."
        bash "$SCRIPT_DIR/download_mainnet_blocks.sh" "$TEST_DATA_DIR/mainnet_blocks"
    else
        echo "Warning: download_mainnet_blocks.sh not found"
        echo "  Blocks can be downloaded manually using Blockstream API or Bitcoin Core RPC"
    fi
    
    echo ""
fi

# Download checkpoint data
if [ "$DOWNLOAD_ALL" = true ] || [ "$DOWNLOAD_CHECKPOINTS" = true ]; then
    echo "=== Downloading Checkpoint Data ==="
    
    CHECKPOINTS_DIR="$TEST_DATA_DIR/checkpoints"
    
    # Checkpoint data is generated from blockchain sync (not downloadable from public API)
    # Create directory with README documenting format and generation
    if [ ! -f "$CHECKPOINTS_DIR/README.md" ]; then
        cat > "$CHECKPOINTS_DIR/README.md" << 'EOF'
# UTXO Set Checkpoints

This directory contains UTXO set checkpoint hashes at specific block heights.

## Format

Checkpoints are stored as JSON files:
```json
{
  "height": 100000,
  "utxo_set_hash": "hex_string",
  "block_hash": "hex_string",
  "timestamp": 1234567890
}
```

## Generating Checkpoints

Checkpoints can be generated by:
1. Running historical block replay with checkpoint verification
2. Using Bitcoin Core's `gettxoutsetinfo` RPC at specific heights
3. Calculating from known UTXO set snapshots

## Usage

Checkpoints are used in `tests/integration/historical_replay.rs` to verify
UTXO set correctness during block replay.
EOF
        echo "✅ Created checkpoint directory structure"
    else
        echo "✅ Checkpoint directory already exists"
    fi
    
    echo ""
fi

# Summary
echo "=== Download Summary ==="
echo ""
echo "Test data directory: $TEST_DATA_DIR"
echo ""

if [ "$DOWNLOAD_ALL" = true ] || [ "$DOWNLOAD_CORE" = true ]; then
    echo "Core Vectors:"
    ls -lh "$TEST_DATA_DIR/core_vectors/transactions/" 2>/dev/null | grep -E "\.json$" || echo "  No files found"
    echo ""
fi

if [ "$DOWNLOAD_ALL" = true ] || [ "$DOWNLOAD_MAINNET" = true ]; then
    echo "Mainnet Blocks:"
    ls -lh "$TEST_DATA_DIR/mainnet_blocks/" 2>/dev/null | grep -E "block_.*\.(hex|bin)$" || echo "  No files found"
    echo ""
fi

if [ "$DOWNLOAD_ALL" = true ] || [ "$DOWNLOAD_CHECKPOINTS" = true ]; then
    echo "Checkpoints:"
    ls -lh "$TEST_DATA_DIR/checkpoints/" 2>/dev/null || echo "  No files found"
    echo ""
fi

echo "=== Next Steps ==="
echo ""
echo "1. Verify downloaded test data is valid"
echo "2. Run tests: cargo test"
echo "3. Check test_data/README.md for usage instructions"
echo ""

