#!/usr/bin/env bash
# Calculate CCIP Chain Selector for ChainID 138
#
# CCIP chain selectors are typically calculated using a deterministic method.
# For custom chains, we can use a simple format: chainId as uint64
# However, for compatibility with Chainlink's official CCIP, we should use
# a format that matches their calculation method.

set -e

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

CHAIN_ID=138

echo "Calculating CCIP Chain Selector for ChainID ${CHAIN_ID}..."
echo ""

# Method 1: Simple chainId as uint64 (for custom CCIP routers)
# This is the simplest approach for custom implementations
SIMPLE_SELECTOR=$CHAIN_ID
echo "Method 1 (Simple): ChainID as uint64"
echo "  Selector: ${SIMPLE_SELECTOR}"
echo "  Hex: 0x$(printf '%016x' ${SIMPLE_SELECTOR})"
echo ""

# Method 2: Chainlink-style calculation (if using official CCIP format)
# Chainlink uses: uint64(uint256(keccak256(abi.encodePacked(chainId, "CCIP")))) >> 192
# For now, we'll use a deterministic calculation based on chainId
# Note: This is a placeholder - actual Chainlink calculation may differ
echo "Method 2 (Chainlink-style): Deterministic hash-based"
echo "  Note: For custom CCIP routers, you can define your own format"
echo "  Recommended: Use Method 1 (simple chainId) for custom implementations"
echo ""

# Method 3: Using cast to calculate (if foundry/cast is available)
if command -v cast &> /dev/null; then
    echo "Method 3 (Using cast):"
    # Calculate using cast: cast keccak "0x$(printf '%064x' ${CHAIN_ID})CCIP"
    HASH=$(cast keccak "$(printf '0x%064x' ${CHAIN_ID})CCIP" 2>/dev/null || echo "")
    if [ -n "$HASH" ]; then
        # Extract first 8 bytes (uint64) from hash
        SELECTOR_HEX=$(echo "$HASH" | cut -c3-18)  # Skip 0x, take 16 hex chars
        SELECTOR_DEC=$(printf '%d' "0x${SELECTOR_HEX}")
        echo "  Hash: ${HASH}"
        echo "  Selector (first 8 bytes): ${SELECTOR_DEC}"
        echo "  Hex: 0x${SELECTOR_HEX}"
    fi
    echo ""
fi

# Recommended selector for ChainID 138
# For custom CCIP routers, using chainId directly is simplest and most compatible
RECOMMENDED_SELECTOR=$CHAIN_ID
RECOMMENDED_SELECTOR_HEX=$(printf '0x%016x' ${CHAIN_ID})

echo "=========================================="
echo "RECOMMENDED SELECTOR FOR CHAINID 138:"
echo "=========================================="
echo "Decimal: ${RECOMMENDED_SELECTOR}"
echo "Hex: ${RECOMMENDED_SELECTOR_HEX}"
echo "Hex (padded): 0x000000000000008a"
echo ""
echo "This selector should be used in:"
echo "  - Environment variables (CHAIN138_SELECTOR)"
echo "  - Bridge destination configuration"
echo "  - Router supported chain registration"
echo "  - All CCIP message routing"
echo ""
echo "Note: If integrating with Chainlink's official CCIP network,"
echo "      you may need to register this chain selector with Chainlink"
echo "      or use their official selector format."
echo ""

# Update .env if it exists
if [ -f "$PROJECT_ROOT/.env" ]; then
    if grep -q "^CHAIN138_SELECTOR=" "$PROJECT_ROOT/.env"; then
        echo "Updating CHAIN138_SELECTOR in .env..."
        sed -i.bak "s|^CHAIN138_SELECTOR=.*|CHAIN138_SELECTOR=${RECOMMENDED_SELECTOR_HEX}|" "$PROJECT_ROOT/.env"
        echo "✅ Updated .env with selector: ${RECOMMENDED_SELECTOR_HEX}"
    else
        echo "Adding CHAIN138_SELECTOR to .env..."
        echo "CHAIN138_SELECTOR=${RECOMMENDED_SELECTOR_HEX}" >> "$PROJECT_ROOT/.env"
        echo "✅ Added CHAIN138_SELECTOR to .env"
    fi
fi

