#!/usr/bin/env bash
# Deploy to all ready chains (7 chains, 31 contracts)
# This script deploys contracts to chains with sufficient wallet balances

set -euo pipefail

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

cd "$PROJECT_ROOT"
# Load .env via dotenv (RPC CR/LF trim). Fallback: raw source.
if [[ -f "$SCRIPT_DIR/../lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/../lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "${PROJECT_ROOT:-$REPO_ROOT}"
elif [[ -n "${PROJECT_ROOT:-}" && -f "$PROJECT_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$PROJECT_ROOT/.env"
  set +a
elif [[ -n "${REPO_ROOT:-}" && -f "$REPO_ROOT/.env" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$REPO_ROOT/.env"
  set +a
fi

# Source environment
if [ -f .env ]; then
    source .env
fi

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

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}Multichain Deployment - Ready Chains${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Check prerequisites
if [ -z "${PRIVATE_KEY:-}" ]; then
    echo -e "${RED}✗ PRIVATE_KEY not set in .env${NC}"
    exit 1
fi

echo -e "${GREEN}✓ Prerequisites check passed${NC}"
echo ""

# Deployment function
deploy_chain() {
    local chain_name=$1
    local rpc_name=$2
    local chain_id=$3
    local script_name=$4
    
    echo -e "${YELLOW}Deploying to ${chain_name}...${NC}"
    echo "  RPC: ${rpc_name}"
    echo "  Chain ID: ${chain_id}"
    echo ""
    
    forge script "${script_name}" \
        --rpc-url "${rpc_name}" \
        --chain-id "${chain_id}" \
        --private-key "${PRIVATE_KEY}" \
        --broadcast \
        --verify \
        -vvvv || {
        echo -e "${RED}✗ Deployment to ${chain_name} failed${NC}"
        return 1
    }
    
    echo -e "${GREEN}✓ ${chain_name} deployment complete${NC}"
    echo ""
}

# Deploy to ready chains
echo -e "${BLUE}Starting deployments...${NC}"
echo ""

# 1. Ethereum Mainnet (CCIPLogger only)
deploy_chain "Ethereum Mainnet" "mainnet" "1" "script/DeployCCIPLoggerOnly.s.sol:DeployCCIPLoggerOnly"

# 2. BSC (all contracts)
deploy_chain "BSC" "bsc" "56" "script/DeployAll.s.sol:DeployAll"

# 3. Polygon (all contracts)
deploy_chain "Polygon" "polygon" "137" "script/DeployAll.s.sol:DeployAll"

# 4. Avalanche (all contracts)
deploy_chain "Avalanche" "avalanche" "43114" "script/DeployAll.s.sol:DeployAll"

# 5. Base (all contracts)
deploy_chain "Base" "base" "8453" "script/DeployAll.s.sol:DeployAll"

# 6. Arbitrum (all contracts)
deploy_chain "Arbitrum" "arbitrum" "42161" "script/DeployAll.s.sol:DeployAll"

# 7. Optimism (all contracts)
deploy_chain "Optimism" "optimism" "10" "script/DeployAll.s.sol:DeployAll"

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}All Deployments Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
