#!/bin/bash

echo "🔍 Verifying Blockchain Integration"
echo "===================================="
echo ""

# Counter for checks
PASSED=0
FAILED=0

# Function to check file exists
check_file() {
  if [ -f "$1" ]; then
    echo "✅ $2"
    ((PASSED++))
  else
    echo "❌ $2 (not found)"
    ((FAILED++))
  fi
}

# Function to check for string in file
check_content() {
  if grep -q "$2" "$1" 2>/dev/null; then
    echo "✅ $3"
    ((PASSED++))
  else
    echo "❌ $3"
    ((FAILED++))
  fi
}

echo "📄 Phase 1: Smart Contract Documentation"
echo "----------------------------------------"
check_file "backend/app/blockchain/contracts/BetEscrow.pseudocode.md" "BetEscrow contract"
check_file "backend/app/blockchain/contracts/BetOracle.pseudocode.md" "BetOracle contract"
check_file "backend/app/blockchain/contracts/README.md" "Architecture README"
echo ""

echo "🔧 Phase 2: Backend Services"
echo "----------------------------"
check_file "backend/app/blockchain/services/blockchain_service.py" "blockchain_service.py"
check_file "backend/app/blockchain/services/blockchain_indexer.py" "blockchain_indexer.py"
check_file "backend/app/blockchain/services/oracle_node.py" "oracle_node.py"
check_file "backend/app/blockchain/services/oracle_aggregator.py" "oracle_aggregator.py"
check_file "backend/app/blockchain/config.py" "Blockchain config"
echo ""

echo "⚛️  Phase 3: Frontend Web3 Hooks"
echo "--------------------------------"
check_file "frontend/src/blockchain/hooks/useWeb3Wallet.ts" "useWeb3Wallet hook"
check_file "frontend/src/blockchain/hooks/useBlockchainBet.ts" "useBlockchainBet hook"
check_file "frontend/src/blockchain/hooks/useGasEstimate.ts" "useGasEstimate hook"
check_file "frontend/src/blockchain/components/BlockchainBadge.tsx" "BlockchainBadge component"
check_file "frontend/src/blockchain/components/TransactionModal.tsx" "TransactionModal component"
echo ""

echo "🎨 Phase 4: UI Component Modifications"
echo "--------------------------------------"
check_content "frontend/src/components/bets/BetCard.tsx" "BlockchainBadgeCompact" "BetCard - Blockchain badge imported"
check_content "frontend/src/components/bets/BetCard.tsx" "blockchain_tx_hash" "BetCard - Conditional blockchain badge"
check_content "frontend/src/components/bets/CreateBetModal.tsx" "useGasEstimate" "CreateBetModal - Gas estimate hook"
check_content "frontend/src/components/bets/CreateBetModal.tsx" "TransactionModal" "CreateBetModal - Transaction modal"
check_content "frontend/src/pages/BetDetails.tsx" "useBlockchainBet" "BetDetails - Blockchain bet hook"
check_content "frontend/src/pages/BetDetails.tsx" "Estimated Gas Cost" "BetDetails - Gas estimate panel"
check_content "frontend/src/components/wallet/WalletBalance.tsx" "On-Chain Escrow" "WalletBalance - On-chain section"
check_content "frontend/src/components/wallet/WalletBalance.tsx" "useWeb3Wallet" "WalletBalance - Web3 wallet hook"
check_content "frontend/src/components/layout/Header.tsx" "Connect Wallet" "Header - Wallet connect button"
check_content "frontend/src/components/layout/Header.tsx" "useWeb3Wallet" "Header - Web3 wallet integration"
echo ""

echo "📝 Phase 5: Database Models"
echo "---------------------------"
check_content "backend/app/models/bet.py" "blockchain_bet_id" "Bet model - blockchain_bet_id field"
check_content "backend/app/models/bet.py" "blockchain_tx_hash" "Bet model - blockchain_tx_hash field"
check_content "backend/app/models/bet.py" "blockchain_status" "Bet model - blockchain_status field"
check_content "backend/app/models/wallet.py" "blockchain_escrow" "Wallet model - blockchain_escrow field"
check_content "frontend/src/types/index.ts" "blockchain_tx_hash" "TypeScript types - Bet blockchain fields"
check_content "frontend/src/types/index.ts" "blockchain_escrow" "TypeScript types - Wallet blockchain field"
echo ""

echo "===================================="
echo "📊 Verification Results"
echo "===================================="
echo "✅ Passed: $PASSED"
echo "❌ Failed: $FAILED"
echo ""

if [ $FAILED -eq 0 ]; then
  echo "🎉 All blockchain integration checks passed!"
  exit 0
else
  echo "⚠️  Some checks failed. Review the output above."
  exit 1
fi
