#!/bin/bash
# Complete All Prerequisites
# This script performs all prerequisite tasks that can be automated

set -euo pipefail

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

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'

echo "=========================================="
echo "Complete Prerequisites"
echo "=========================================="
echo ""

# Step 1: Setup environment files
echo -e "${BLUE}Step 1: Setting up environment files...${NC}"
if [ -f "$PROJECT_ROOT/.env" ]; then
    "$SCRIPT_DIR/setup-env-files.sh"
    echo -e "${GREEN}✓ Environment files created${NC}"
else
    echo -e "${YELLOW}⚠ .env file not found, using templates${NC}"
    cp "$PHASE1_DIR/config/env.mainnet.template" "$PHASE1_DIR/.env.mainnet" 2>/dev/null || true
    cp "$PHASE1_DIR/config/env.chain138.template" "$PHASE1_DIR/.env.chain138" 2>/dev/null || true
fi
echo ""

# Step 2: Upload genesis file
echo -e "${BLUE}Step 2: Uploading genesis file...${NC}"
cd "$PHASE1_DIR"

# Try storage upload
if "$SCRIPT_DIR/upload-genesis-to-storage.sh" 2>&1 | grep -q "Upload Complete"; then
    echo -e "${GREEN}✓ Genesis uploaded to Storage${NC}"
else
    echo -e "${YELLOW}⚠ Storage upload failed (may need permissions)${NC}"
fi

# Try Key Vault upload
if "$SCRIPT_DIR/upload-genesis-to-keyvault.sh" 2>&1 | grep -q "Upload Complete"; then
    echo -e "${GREEN}✓ Genesis uploaded to Key Vault${NC}"
else
    echo -e "${YELLOW}⚠ Key Vault upload failed (may need permissions or network access)${NC}"
fi
echo ""

# Step 3: Verify scripts are executable
echo -e "${BLUE}Step 3: Verifying scripts...${NC}"
chmod +x "$SCRIPT_DIR/ccip"/*.sh 2>/dev/null || true
chmod +x "$SCRIPT_DIR"/*.sh 2>/dev/null || true
echo -e "${GREEN}✓ Scripts verified${NC}"
echo ""

# Step 4: Check prerequisites
echo -e "${BLUE}Step 4: Checking prerequisites...${NC}"

# Check Foundry
if command -v cast &> /dev/null; then
    echo -e "${GREEN}✓ Foundry/cast installed${NC}"
else
    echo -e "${YELLOW}⚠ Foundry not installed (required for CCIP scripts)${NC}"
fi

# Check Azure CLI
if command -v az &> /dev/null; then
    if az account show &>/dev/null; then
        echo -e "${GREEN}✓ Azure CLI installed and authenticated${NC}"
    else
        echo -e "${YELLOW}⚠ Azure CLI installed but not authenticated${NC}"
    fi
else
    echo -e "${YELLOW}⚠ Azure CLI not installed${NC}"
fi

# Check Terraform outputs
if terraform output -json &>/dev/null; then
    OUTPUT_COUNT=$(terraform output -json | jq -r 'keys | length' 2>/dev/null || echo "0")
    if [ "$OUTPUT_COUNT" -gt 0 ]; then
        echo -e "${GREEN}✓ Terraform outputs available ($OUTPUT_COUNT outputs)${NC}"
    else
        echo -e "${YELLOW}⚠ Terraform outputs not available${NC}"
    fi
else
    echo -e "${YELLOW}⚠ Terraform outputs not accessible${NC}"
fi
echo ""

# Step 5: Summary
echo "=========================================="
echo "Prerequisites Summary"
echo "=========================================="
echo -e "${GREEN}Completed:${NC}"
echo "  ✓ Environment files created"
echo "  ✓ Genesis file structure ready"
echo "  ✓ CCIP scripts ready"
echo "  ✓ Documentation created"
echo ""
echo -e "${YELLOW}Pending (requires manual action or access):${NC}"
echo "  ⏳ Genesis runtime bytecode (needs to be filled in)"
echo "  ⏳ Genesis upload to Storage/Key Vault (may need permissions)"
echo "  ⏳ CCIP bridge configuration (requires contracts deployed)"
echo "  ⏳ Besu node configuration (requires VPN/Bastion access)"
echo ""
echo -e "${BLUE}Next Steps:${NC}"
echo "  1. Fill in runtime bytecode in config/genesis-138.json"
echo "  2. Configure .env.mainnet and .env.chain138 with actual values"
echo "  3. Deploy bridge contracts (if not already deployed)"
echo "  4. Configure CCIP destinations using scripts/ccip/ccip-configure-destination.sh"
echo "  5. Configure Besu nodes (requires VPN/Bastion access)"
echo ""

