#!/bin/bash
# Deploy Bridge Contracts to Chain 138

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
FORGE_SCOPE_RUNNER=(bash "$PROJECT_ROOT/scripts/forge/scope.sh")
RPC_URL="${CHAIN_138_RPC_URL:-http://localhost:8545}"
PRIVATE_KEY="${DEPLOYER_PRIVATE_KEY}"
ADMIN_ADDRESS="${ADMIN_ADDRESS}"

if [ -z "$PRIVATE_KEY" ]; then
    echo -e "${RED}Error: DEPLOYER_PRIVATE_KEY not set${NC}"
    exit 1
fi

if [ -z "$ADMIN_ADDRESS" ]; then
    echo -e "${RED}Error: ADMIN_ADDRESS not set${NC}"
    exit 1
fi

echo -e "${GREEN}Deploying Bridge Contracts to Chain 138...${NC}"
echo "RPC URL: $RPC_URL"
echo "Admin Address: $ADMIN_ADDRESS"

# Deploy contracts
echo -e "${YELLOW}Deploying BridgeRegistry...${NC}"
REGISTRY_ADDRESS=$("${FORGE_SCOPE_RUNNER[@]}" script script/bridge/interop/DeployBridgeRegistry.s.sol \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --broadcast \
    --json | jq -r '.returns.registry.value')

echo "BridgeRegistry deployed at: $REGISTRY_ADDRESS"

echo -e "${YELLOW}Deploying BridgeEscrowVault...${NC}"
VAULT_ADDRESS=$("${FORGE_SCOPE_RUNNER[@]}" script script/bridge/interop/DeployBridgeEscrowVault.s.sol \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --broadcast \
    --json \
    --sig "run(address,address)" "$ADMIN_ADDRESS" "$REGISTRY_ADDRESS" | jq -r '.returns.vault.value')

echo "BridgeEscrowVault deployed at: $VAULT_ADDRESS"

echo -e "${YELLOW}Deploying wXRP Token...${NC}"
WXRP_ADDRESS=$("${FORGE_SCOPE_RUNNER[@]}" script script/bridge/interop/DeployWXRP.s.sol \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --broadcast \
    --json \
    --sig "run(address)" "$ADMIN_ADDRESS" | jq -r '.returns.wxrp.value')

echo "wXRP deployed at: $WXRP_ADDRESS"

echo -e "${YELLOW}Deploying MintBurnController...${NC}"
if [ -z "$HSM_SIGNER_ADDRESS" ]; then
    echo -e "${YELLOW}Warning: HSM_SIGNER_ADDRESS not set, using admin address${NC}"
    HSM_SIGNER_ADDRESS="$ADMIN_ADDRESS"
fi

CONTROLLER_ADDRESS=$("${FORGE_SCOPE_RUNNER[@]}" script script/bridge/interop/DeployMintBurnController.s.sol \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --broadcast \
    --json \
    --sig "run(address,address,address)" "$ADMIN_ADDRESS" "$WXRP_ADDRESS" "$HSM_SIGNER_ADDRESS" | jq -r '.returns.controller.value')

echo "MintBurnController deployed at: $CONTROLLER_ADDRESS"

echo -e "${YELLOW}Deploying BridgeVerifier...${NC}"
VERIFIER_ADDRESS=$("${FORGE_SCOPE_RUNNER[@]}" script script/bridge/interop/DeployBridgeVerifier.s.sol \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --broadcast \
    --json \
    --sig "run(address,uint256)" "$ADMIN_ADDRESS" 6667 | jq -r '.returns.verifier.value')

echo "BridgeVerifier deployed at: $VERIFIER_ADDRESS"

# Save addresses to file
cat > .bridge-deployment.json <<EOF
{
  "chainId": 138,
  "deployedAt": $(date +%s),
  "contracts": {
    "BridgeRegistry": "$REGISTRY_ADDRESS",
    "BridgeEscrowVault": "$VAULT_ADDRESS",
    "wXRP": "$WXRP_ADDRESS",
    "MintBurnController": "$CONTROLLER_ADDRESS",
    "BridgeVerifier": "$VERIFIER_ADDRESS"
  },
  "admin": "$ADMIN_ADDRESS",
  "hsmSigner": "$HSM_SIGNER_ADDRESS"
}
EOF

echo -e "${GREEN}Deployment complete!${NC}"
echo "Addresses saved to .bridge-deployment.json"

# Initialize registry with default destinations
echo -e "${YELLOW}Initializing registry...${NC}"
"${FORGE_SCOPE_RUNNER[@]}" script script/bridge/interop/InitializeRegistry.s.sol \
    --rpc-url "$RPC_URL" \
    --private-key "$PRIVATE_KEY" \
    --broadcast \
    --sig "run(address)" "$REGISTRY_ADDRESS"

echo -e "${GREEN}Registry initialized!${NC}"
