#!/bin/bash
# Verify tiered architecture implementation

set -euo pipefail

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

echo "=== Verifying Tiered Architecture Implementation ==="
echo ""

ERRORS=0
WARNINGS=0

# Check 1: Database migration files exist
echo -n "Checking migration files... "
if [ -f "$PROJECT_ROOT/backend/database/migrations/0010_track_schema.up.sql" ] && \
   [ -f "$PROJECT_ROOT/backend/database/migrations/0010_track_schema.auth_only.sql" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 2: Feature flags implementation
echo -n "Checking feature flags... "
if [ -f "$PROJECT_ROOT/backend/featureflags/flags.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 3: Wallet auth
echo -n "Checking wallet authentication... "
if [ -f "$PROJECT_ROOT/backend/auth/wallet_auth.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 4: Auth middleware
echo -n "Checking auth middleware... "
if [ -f "$PROJECT_ROOT/backend/api/middleware/auth.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 5: Track implementations
echo -n "Checking Track 1... "
if [ -f "$PROJECT_ROOT/backend/api/track1/endpoints.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

echo -n "Checking Track 2... "
if [ -f "$PROJECT_ROOT/backend/api/track2/endpoints.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

echo -n "Checking Track 3... "
if [ -f "$PROJECT_ROOT/backend/api/track3/endpoints.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

echo -n "Checking Track 4... "
if [ -f "$PROJECT_ROOT/backend/api/track4/endpoints.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 6: Indexers
echo -n "Checking Track 2 indexers... "
if [ -f "$PROJECT_ROOT/backend/indexer/track2/block_indexer.go" ] && \
   [ -f "$PROJECT_ROOT/backend/indexer/track2/tx_indexer.go" ] && \
   [ -f "$PROJECT_ROOT/backend/indexer/track2/token_indexer.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 7: Analytics
echo -n "Checking analytics engine... "
if [ -f "$PROJECT_ROOT/backend/analytics/flow_tracker.go" ] && \
   [ -f "$PROJECT_ROOT/backend/analytics/bridge_analytics.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 8: Route integration
echo -n "Checking route integration... "
if [ -f "$PROJECT_ROOT/backend/api/rest/track_routes.go" ]; then
    echo "✅"
else
    echo "❌"
    ((ERRORS++))
fi

# Check 9: Documentation
echo -n "Checking documentation... "
if [ -f "$PROJECT_ROOT/docs/api/track-api-contracts.md" ] && \
   [ -f "$PROJECT_ROOT/docs/feature-flags/track-feature-matrix.md" ]; then
    echo "✅"
else
    echo "⚠️"
    ((WARNINGS++))
fi

# Check 10: Setup scripts
echo -n "Checking setup scripts... "
if [ -f "$PROJECT_ROOT/scripts/setup-tiered-architecture.sh" ] && \
   [ -f "$PROJECT_ROOT/scripts/run-migration-0010.sh" ]; then
    echo "✅"
else
    echo "⚠️"
    ((WARNINGS++))
fi

echo ""
echo "=== Summary ==="
echo "Errors: $ERRORS"
echo "Warnings: $WARNINGS"

if [ $ERRORS -eq 0 ]; then
    echo ""
    echo "✅ All critical components verified!"
    echo ""
    echo "Next steps:"
    echo "1. Run: bash scripts/setup-tiered-architecture.sh"
    echo "2. Export DB_PASSWORD and run: bash scripts/run-migration-0010.sh"
    echo "   (auto-detects standalone explorer DB vs shared Blockscout DB)"
    echo "3. Set JWT_SECRET environment variable"
    echo "4. Start the API server"
    exit 0
else
    echo ""
    echo "❌ Some components are missing. Please review errors above."
    exit 1
fi
