#!/bin/bash
# Setup Environment Files from .env
# Creates .env.mainnet and .env.chain138 from project .env file

set -euo pipefail

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

if [ ! -f "$ENV_FILE" ]; then
    echo "Error: .env file not found at $ENV_FILE"
    exit 1
fi

echo "=========================================="
echo "Setup Environment Files"
echo "=========================================="
echo "Source: $ENV_FILE"
echo ""

# Load .env file
export $(grep -v '^#' "$ENV_FILE" | grep -E "^(RPC_URL|CHAIN_ID|PRIVATE_KEY|MIRROR_ADMIN|CCIP_ROUTER|LINK_TOKEN|WETH9_ADDRESS|WETH10_ADDRESS|CCIPWETH9_BRIDGE|CCIPWETH10_BRIDGE|ETHEREUM_MAINNET_SELECTOR|CHAIN138_SELECTOR|ETHERSCAN|CLOUDFLARE_DOMAIN)" | xargs)

# Create .env.mainnet
echo "Creating .env.mainnet..."
cat > "$PHASE1_DIR/.env.mainnet" <<EOF
# Ethereum Mainnet Environment Configuration
# Generated from project .env file

# Network
RPC_URL=${RPC_URL:-https://mainnet.infura.io/v3/<YOUR_KEY>}
CHAIN_ID=1

# Deployer / Admin
PRIVATE_KEY=${PRIVATE_KEY:-}
MIRROR_ADMIN=${MIRROR_ADMIN:-0xa55A4B57A91561e9df5a883D4883Bd4b1a7C4882}

# CCIP Router & LINK Token
CCIP_ROUTER=${CCIP_ROUTER:-0x80226fc0Ee2b096224EeAc085Bb9a8cba1146f7D}
LINK_TOKEN=${LINK_TOKEN:-0x514910771AF9Ca656af840dff83E8264EcF986CA}

# Tokens
WETH9_ADDRESS=${WETH9_ADDRESS:-0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2}
WETH10_ADDRESS=${WETH10_ADDRESS:-0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f}

# Bridges
CCIPWETH9_BRIDGE_MAINNET=${CCIPWETH9_BRIDGE_MAINNET:-0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6}
CCIPWETH10_BRIDGE_MAINNET=${CCIPWETH10_BRIDGE_MAINNET:-0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e}

# CCIP Chain Selectors
ETHEREUM_MAINNET_SELECTOR=${ETHEREUM_MAINNET_SELECTOR:-5009297550715157269}
CHAIN138_SELECTOR=${CHAIN138_SELECTOR:-}

# Explorers
ETHERSCAN_API_URL=${ETHERSCAN_API_URL:-https://api.etherscan.io/api}
ETHERSCAN_API_KEY=${ETHERSCAN_API_KEY:-}

# Fee Configuration
FEE_TOKEN=${FEE_TOKEN:-${LINK_TOKEN:-0x514910771AF9Ca656af840dff83E8264EcF986CA}}
EOF

# Create .env.chain138
echo "Creating .env.chain138..."
cat > "$PHASE1_DIR/.env.chain138" <<EOF
# ChainID 138 Environment Configuration
# Generated from project .env file

# Network
RPC_URL=${RPC_URL:-https://rpc.d-bis.org}
CHAIN_ID=138

# Deployer / Admin
PRIVATE_KEY=${PRIVATE_KEY:-}
MIRROR_ADMIN=${MIRROR_ADMIN:-0xa55A4B57A91561e9df5a883D4883Bd4b1a7C4882}

# CCIP Router & LINK Token
CCIP_ROUTER=${CCIP_ROUTER:-0x80226fc0Ee2b096224EeAc085Bb9a8cba1146f7D}
LINK_TOKEN=${LINK_TOKEN:-0x514910771AF9Ca656af840dff83E8264EcF986CA}

# Tokens (deterministic same addresses)
WETH9_ADDRESS=${WETH9_ADDRESS:-0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2}
WETH10_ADDRESS=${WETH10_ADDRESS:-0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f}

# Bridges
CCIPWETH9_BRIDGE_CHAIN138=${CCIPWETH9_BRIDGE_CHAIN138:-0x971cD9D156f193df8051E48043C476e53ECd4693}
CCIPWETH10_BRIDGE_CHAIN138=${CCIPWETH10_BRIDGE_CHAIN138:-0xe0E93247376aa097dB308B92e6Ba36bA015535D0}

# CCIP Chain Selectors
ETHEREUM_MAINNET_SELECTOR=${ETHEREUM_MAINNET_SELECTOR:-5009297550715157269}
CHAIN138_SELECTOR=${CHAIN138_SELECTOR:-}

# Explorer
CHAIN138_ETHERSCAN_API_URL=${CHAIN138_ETHERSCAN_API_URL:-}
CHAIN138_ETHERSCAN_API_KEY=${CHAIN138_ETHERSCAN_API_KEY:-}

# Fee Configuration
FEE_TOKEN=${FEE_TOKEN:-${LINK_TOKEN:-0x514910771AF9Ca656af840dff83E8264EcF986CA}}
EOF

echo "✓ Environment files created:"
echo "  - .env.mainnet"
echo "  - .env.chain138"
echo ""
echo "Review and update these files with any missing values."
