#!/bin/bash

# Update Ostium contracts from GitHub
# Usage: ./scripts/update_contracts.sh [--force]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

echo "🔄 Updating Ostium contracts from GitHub repository..."

# Change to project directory
cd "$PROJECT_ROOT"

# Check if --force flag is provided
FORCE=false
if [[ "$1" == "--force" ]]; then
    FORCE=true
    echo "🚀 Force update enabled"
fi

# Remove existing generated contracts if force update
if [[ "$FORCE" == "true" ]]; then
    echo "🗑️  Removing existing generated contracts..."
    rm -rf src/contracts/generated/
fi

# Build with contract update feature
echo "🛠️  Building with contract updates..."
cargo build --features update-contracts

# Run the dynamic contracts example to verify
echo "✅ Testing dynamic contract fetching..."
cargo run --example dynamic_contracts

echo "🎉 Contract update complete!"
echo ""
echo "To use the updated contracts in your code:"
echo "  use ostium_rust_sdk::contracts::generated::*;"
echo ""
echo "Or use the runtime fetcher:"
echo "  use ostium_rust_sdk::contracts::fetcher::ContractFetcher;" 