#!/usr/bin/env bash
# Retry contract verification after Blockscout is started
# Usage: ./retry-contract-verification.sh

echo "========================================="
echo "Contract Verification Retry"
echo "========================================="
echo ""

# Check Blockscout API first
echo "1. Checking Blockscout API accessibility..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 10 "https://explorer.d-bis.org/api" 2>/dev/null || echo "000")

if [[ "$HTTP_CODE" == "200" ]]; then
    echo "   ✅ Blockscout API is accessible (HTTP $HTTP_CODE)"
    echo ""
    echo "2. Starting contract verification..."
    echo ""
    
    # Run verification script
    cd /home/intlc/projects/proxmox
    ./scripts/verify-all-contracts.sh 0.8.20
    
elif [[ "$HTTP_CODE" == "502" ]] || [[ "$HTTP_CODE" == "000" ]]; then
    echo "   ⚠️  Blockscout API is not accessible (HTTP $HTTP_CODE)"
    echo "   💡 Start Blockscout service first:"
    echo "      ./scripts/start-blockscout.sh"
    echo ""
    echo "   Or manually on pve2:"
    echo "      ssh root@pve2 'pct exec 5000 -- systemctl start blockscout'"
    exit 1
else
    echo "   ⚠️  Blockscout API returned HTTP $HTTP_CODE"
    echo "   Proceeding with verification attempt anyway..."
    echo ""
    cd /home/intlc/projects/proxmox
    ./scripts/verify-all-contracts.sh 0.8.20
fi
