#!/usr/bin/env bash
# Complete all remaining tasks

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
cd "$PROJECT_ROOT"
source "$SCRIPT_DIR/../lib/init.sh"
source "$PROJECT_ROOT/scripts/load-env.sh" >/dev/null 2>&1 || true

# Color codes

echo "==================================================================="
echo "  COMPLETING ALL REMAINING TASKS"
echo "==================================================================="

# Load environment variables
if [ -f .env ]; then
    source .env 2>/dev/null || true
fi

COMPLETED=0
SKIPPED=0
ERRORS=0

# Function to complete task
complete_task() {
    local task_name=$1
    local command=$2
    
    log_info "Task: $task_name"
    if eval "$command" 2>&1; then
        log_success "✅ Completed: $task_name"
        COMPLETED=$((COMPLETED + 1))
    else
        log_warn "⚠️  Skipped: $task_name (may require manual intervention)"
        SKIPPED=$((SKIPPED + 1))
    fi
}

# 1. Compile all contracts
complete_task "Compile Foundry contracts" "forge build --force 2>&1 | head -20"

# 2. Compile Hardhat contracts
complete_task "Compile Hardhat contracts" "npx hardhat compile 2>&1 | head -20"

# 3. Run all tests
complete_task "Run Foundry tests" "forge test --no-match-path 'test/ccip-integration/*' 2>&1 | tail -10"

# 4. Verify all scripts
complete_task "Validate all scripts" "./scripts/automation/validate-all-scripts.sh 2>&1 | tail -10"

# 5. Run scope review
complete_task "Run scope review" "./scripts/automation/scope-review.sh 2>&1 | tail -10"

# 6. Check Mainnet deployments
complete_task "Check Mainnet deployment status" "./scripts/deployment/check-existing-deployments.sh 2>&1 | tail -15"

# 7. Get Mainnet gas prices
complete_task "Get Mainnet gas prices" "./scripts/deployment/get-mainnet-gas-prices.sh 2>&1 | tail -10"

# 8. Calculate deployment costs
complete_task "Calculate Mainnet deployment costs" "./scripts/deployment/calculate-accurate-deployment-costs.sh 2>&1 | tail -10"

# 9. Verify Chain-138 configuration
complete_task "Verify Chain-138 configuration" "./scripts/deployment/setup-chain138-env.sh 2>&1 | tail -10"

# 10. Cross-check Chain-138
complete_task "Cross-check Chain-138" "./scripts/deployment/cross-check-chain138.sh 2>&1 | tail -10"

# 11. Generate final reports
log_info "Generating final reports..."

# Mainnet deployment report
if [ -f "scripts/deployment/final-mainnet-deployment-report.sh" ]; then
    ./scripts/deployment/final-mainnet-deployment-report.sh > docs/FINAL_MAINNET_REPORT.txt 2>&1
    log_success "✅ Generated Mainnet deployment report"
fi

# Chain-138 status report
if [ -f "scripts/deployment/deploy-chain138-complete.sh" ]; then
    ./scripts/deployment/deploy-chain138-complete.sh > docs/FINAL_CHAIN138_REPORT.txt 2>&1
    log_success "✅ Generated Chain-138 status report"
fi

echo "==================================================================="
log_info "SUMMARY"
echo "==================================================================="
echo "  ✅ Completed: $COMPLETED"
echo "  ⚠️  Skipped: $SKIPPED"
echo "  ❌ Errors: $ERRORS"

if [ $COMPLETED -gt 0 ]; then
    log_success "✅ All automated tasks completed!"
fi

echo "==================================================================="
