#!/usr/bin/env bash
# Full Readiness Check for CCIP Bridge Setup
# Comprehensive validation of all components
set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$PROJECT_ROOT/scripts/lib/ccip-destinations.sh"
source "$PROJECT_ROOT/scripts/lib/address-inventory.sh"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_header() { echo -e "${CYAN}[CHECK]${NC} $1"; }

# Load runtime env plus address inventory.
load_explorer_runtime_env

RPC_URL="${RPC_URL_138:-http://192.168.11.250:8545}"
LINK_TOKEN="$(resolve_address_value LINK_TOKEN LINK_TOKEN 0x73ADaF7dBa95221c080db5631466d2bC54f6a76B)"
WETH9="$(resolve_address_value WETH9_ADDRESS WETH9_ADDRESS 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2)"
WETH10="$(resolve_address_value WETH10_ADDRESS WETH10_ADDRESS 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f)"
WETH9_BRIDGE="$(resolve_address_value CCIPWETH9_BRIDGE CCIPWETH9_BRIDGE 0xcacfd227A040002e49e2e01626363071324f820a)"
WETH10_BRIDGE="$(resolve_address_value CCIPWETH10_BRIDGE CCIPWETH10_BRIDGE 0xe0E93247376aa097dB308B92e6Ba36bA015535D0)"

PASSED=0
FAILED=0
WARNINGS=0

check_pass() { ((PASSED++)); log_success "$1"; }
check_fail() { ((FAILED++)); log_error "$1"; }
check_warn() { ((WARNINGS++)); log_warn "$1"; }

echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     FULL READINESS CHECK - CCIP BRIDGE SETUP                ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

# 1. Network Connectivity
log_header "1. Network Connectivity"
BLOCK=$(cast block-number --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$BLOCK" ] && [ "$BLOCK" != "unknown" ] && [ "$BLOCK" != "0" ]; then
    check_pass "Network connected (Block: $BLOCK)"
else
    check_fail "Network not accessible"
fi

CHAIN_ID=$(cast chain-id --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ "$CHAIN_ID" = "138" ]; then
    check_pass "Chain ID correct: 138"
else
    check_fail "Chain ID incorrect: $CHAIN_ID (expected 138)"
fi
echo ""

# 2. Account Status
log_header "2. Account Status"
if [ -z "${PRIVATE_KEY:-}" ]; then
    check_fail "PRIVATE_KEY not available in effective environment"
    ACCOUNT=""
else
    ACCOUNT=$(cast wallet address "$PRIVATE_KEY" 2>/dev/null || echo "")
    if [ -n "$ACCOUNT" ]; then
        check_pass "Account derived: $ACCOUNT"
        
        ETH_BAL=$(cast balance "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
        ETH_BAL_ETH=$(cast --from-wei "$ETH_BAL" ether 2>/dev/null || echo "0")
        if (( $(echo "$ETH_BAL_ETH >= 0.1" | bc -l 2>/dev/null || echo 1) )); then
            check_pass "ETH balance sufficient: $ETH_BAL_ETH ETH"
        else
            check_fail "ETH balance insufficient: $ETH_BAL_ETH ETH (need 0.1+)"
        fi
        
        NONCE=$(cast nonce "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
        check_pass "Current nonce: $NONCE"
    else
        check_fail "Could not derive account from PRIVATE_KEY"
    fi
fi
echo ""

# 3. LINK Token Contract
log_header "3. LINK Token Contract"
LINK_CODE=$(cast code "$LINK_TOKEN" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$LINK_CODE" ] && [ "$LINK_CODE" != "0x" ] && [ ${#LINK_CODE} -gt 100 ]; then
    check_pass "LINK token deployed at $LINK_TOKEN"
    
    NAME=$(cast call "$LINK_TOKEN" "name()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    if [ -n "$NAME" ] && [ "$NAME" != "0x" ]; then
        check_pass "Token name: $NAME"
    else
        check_warn "Could not read token name"
    fi
    
    SYMBOL=$(cast call "$LINK_TOKEN" "symbol()" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    if [ -n "$SYMBOL" ] && [ "$SYMBOL" != "0x" ]; then
        check_pass "Token symbol: $SYMBOL"
    else
        check_warn "Could not read token symbol"
    fi
else
    check_fail "LINK token not found at $LINK_TOKEN"
fi
echo ""

# 4. Account Token Balances
log_header "4. Account Token Balances"
if [ -n "$ACCOUNT" ] && [ -n "$LINK_CODE" ] && [ "$LINK_CODE" != "0x" ]; then
    LINK_BAL=$(cast call "$LINK_TOKEN" "balanceOf(address)" "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    LINK_BAL_ETH=$(cast --from-wei "$LINK_BAL" ether 2>/dev/null || echo "0")
    if (( $(echo "$LINK_BAL_ETH >= 20" | bc -l 2>/dev/null || echo 0) )); then
        check_pass "LINK balance: $LINK_BAL_ETH LINK (sufficient)"
    else
        check_fail "LINK balance: $LINK_BAL_ETH LINK (need 20+)"
    fi
else
    check_warn "Cannot check LINK balance (account or token not available)"
fi

WETH9_BAL=$(cast call "$WETH9" "balanceOf(address)" "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
WETH9_BAL_ETH=$(cast --from-wei "$WETH9_BAL" ether 2>/dev/null || echo "0")
check_pass "WETH9 balance: $WETH9_BAL_ETH WETH9"

WETH10_BAL=$(cast call "$WETH10" "balanceOf(address)" "$ACCOUNT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
WETH10_BAL_ETH=$(cast --from-wei "$WETH10_BAL" ether 2>/dev/null || echo "0")
check_pass "WETH10 balance: $WETH10_BAL_ETH WETH10"
echo ""

# 5. Bridge Contract Status
log_header "5. Bridge Contract Status"
WETH9_BRIDGE_CODE=$(cast code "$WETH9_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$WETH9_BRIDGE_CODE" ] && [ "$WETH9_BRIDGE_CODE" != "0x" ]; then
    check_pass "WETH9 Bridge deployed: $WETH9_BRIDGE"
else
    check_fail "WETH9 Bridge not found: $WETH9_BRIDGE"
fi

WETH10_BRIDGE_CODE=$(cast code "$WETH10_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
if [ -n "$WETH10_BRIDGE_CODE" ] && [ "$WETH10_BRIDGE_CODE" != "0x" ]; then
    check_pass "WETH10 Bridge deployed: $WETH10_BRIDGE"
else
    check_fail "WETH10 Bridge not found: $WETH10_BRIDGE"
fi
echo ""

# 6. Bridge LINK Balances
log_header "6. Bridge LINK Balances"
if [ -n "$LINK_CODE" ] && [ "$LINK_CODE" != "0x" ]; then
    WETH9_LINK=$(cast call "$LINK_TOKEN" "balanceOf(address)" "$WETH9_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    WETH9_LINK_ETH=$(cast --from-wei "$WETH9_LINK" ether 2>/dev/null || echo "0")
    if (( $(echo "$WETH9_LINK_ETH >= 10" | bc -l 2>/dev/null || echo 0) )); then
        check_pass "WETH9 Bridge LINK: $WETH9_LINK_ETH LINK (sufficient)"
    else
        check_fail "WETH9 Bridge LINK: $WETH9_LINK_ETH LINK (need 10+)"
    fi
    
    WETH10_LINK=$(cast call "$LINK_TOKEN" "balanceOf(address)" "$WETH10_BRIDGE" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
    WETH10_LINK_ETH=$(cast --from-wei "$WETH10_LINK" ether 2>/dev/null || echo "0")
    if (( $(echo "$WETH10_LINK_ETH >= 10" | bc -l 2>/dev/null || echo 0) )); then
        check_pass "WETH10 Bridge LINK: $WETH10_LINK_ETH LINK (sufficient)"
    else
        check_fail "WETH10 Bridge LINK: $WETH10_LINK_ETH LINK (need 10+)"
    fi
else
    check_warn "Cannot check bridge LINK balances (LINK token not available)"
fi
echo ""

# 7. Bridge Destination Configuration
log_header "7. Bridge Destination Configuration"
ETHEREUM_SELECTOR="${ETHEREUM_SELECTOR:-$(ccip_destination_selector_by_name "Ethereum Mainnet")}"
if [ -n "$WETH9_BRIDGE_CODE" ] && [ "$WETH9_BRIDGE_CODE" != "0x" ]; then
    DEST=$(cast call "$WETH9_BRIDGE" "destinations(uint64)" "$ETHEREUM_SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    DEST_HEX=$(echo "$DEST" | sed 's/0x//')
    DEST_CLEAN=""
    if [ ${#DEST_HEX} -ge 128 ]; then
        ADDR_HEX=$(echo "$DEST_HEX" | cut -c65-128)
        DEST_CLEAN="0x${ADDR_HEX:24:40}"
    fi
    if [ -n "$DEST_CLEAN" ] && [ "$DEST_CLEAN" != "0x0000000000000000000000000000000000000000" ]; then
        check_pass "WETH9 Bridge: Ethereum Mainnet configured ($DEST_CLEAN)"
    else
        check_fail "WETH9 Bridge: Ethereum Mainnet NOT configured"
    fi
fi

if [ -n "$WETH10_BRIDGE_CODE" ] && [ "$WETH10_BRIDGE_CODE" != "0x" ]; then
    DEST=$(cast call "$WETH10_BRIDGE" "destinations(uint64)" "$ETHEREUM_SELECTOR" --rpc-url "$RPC_URL" 2>/dev/null || echo "")
    DEST_HEX=$(echo "$DEST" | sed 's/0x//')
    DEST_CLEAN=""
    if [ ${#DEST_HEX} -ge 128 ]; then
        ADDR_HEX=$(echo "$DEST_HEX" | cut -c65-128)
        DEST_CLEAN="0x${ADDR_HEX:24:40}"
    fi
    if [ -n "$DEST_CLEAN" ] && [ "$DEST_CLEAN" != "0x0000000000000000000000000000000000000000" ]; then
        check_pass "WETH10 Bridge: Ethereum Mainnet configured ($DEST_CLEAN)"
    else
        check_fail "WETH10 Bridge: Ethereum Mainnet NOT configured"
    fi
fi
echo ""

# 8. Configuration Files
log_header "8. Configuration Files"
if [ -f "$PROJECT_ROOT/.env" ]; then
    check_pass ".env file exists"
else
    check_warn ".env file not found"
fi
if [ -n "${PRIVATE_KEY:-}" ]; then
    check_pass "PRIVATE_KEY available in effective environment"
else
    check_fail "PRIVATE_KEY not available in effective environment"
fi
if [ -f "$EXPLORER_ADDRESS_INVENTORY_FILE" ] && [ -n "$(inventory_get LINK_TOKEN 2>/dev/null || true)" ]; then
    check_pass "LINK_TOKEN configured in address inventory"
else
    check_warn "LINK_TOKEN not in address inventory"
fi
echo ""

# 9. Script Availability
log_header "9. Script Availability"
SCRIPTS=(
    "force-deploy-link.sh"
    "fund-bridge-contracts.sh"
    "get-funding-report.sh"
    "wrap-and-bridge-to-ethereum.sh"
    "check-bridge-config.sh"
)
for script in "${SCRIPTS[@]}"; do
    if [ -f "$SCRIPT_DIR/$script" ] && [ -x "$SCRIPT_DIR/$script" ]; then
        check_pass "Script available: $script"
    else
        check_warn "Script missing or not executable: $script"
    fi
done
echo ""

# 10. Summary
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║     READINESS CHECK SUMMARY                                  ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo "Passed:  $PASSED"
echo "Failed:  $FAILED"
echo "Warnings: $WARNINGS"
echo ""

if [ $FAILED -eq 0 ] && [ $WARNINGS -eq 0 ]; then
    log_success "✓✓✓ SYSTEM FULLY READY"
    exit 0
elif [ $FAILED -eq 0 ]; then
    log_warn "⚠ SYSTEM READY WITH WARNINGS"
    exit 0
else
    log_error "✗ SYSTEM NOT READY - $FAILED critical issues found"
    exit 1
fi
