#!/bin/bash

# Deploy All Utility Contracts
# This script deploys all utility contracts

set -e

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

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

source "$PROJECT_ROOT/scripts/load-env.sh" >/dev/null 2>&1 || true

if [ ! -f .env ]; then
    echo -e "${RED}Error: .env file not found${NC}"
    exit 1
fi

# Set defaults
RPC_URL=${RPC_URL:-${RPC_URL_138:-"http://192.168.11.250:8545"}}
GAS_PRICE=${GAS_PRICE:-20000000000}

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Deploying All Utility Contracts${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""

# Step 1: Deploy TokenRegistry
echo -e "${YELLOW}Step 1: Deploying TokenRegistry...${NC}"
forge script script/DeployTokenRegistry.s.sol:DeployTokenRegistry \
    --rpc-url "$RPC_URL" \
    --broadcast \
    --legacy \
    --gas-price "$GAS_PRICE" \
    -vv | tee /tmp/token-registry-deploy.log

TOKEN_REGISTRY=$(grep "TokenRegistry deployed at:" /tmp/token-registry-deploy.log | tail -1 | awk '{print $NF}')
if [ -z "$TOKEN_REGISTRY" ]; then
    echo -e "${RED}Error: Failed to extract TokenRegistry address${NC}"
    exit 1
fi

echo -e "${GREEN}TokenRegistry deployed at: $TOKEN_REGISTRY${NC}"
echo "TOKEN_REGISTRY_ADDRESS=$TOKEN_REGISTRY" >> .env
echo ""

# Step 2: Deploy FeeCollector
echo -e "${YELLOW}Step 2: Deploying FeeCollector...${NC}"
forge script script/DeployFeeCollector.s.sol:DeployFeeCollector \
    --rpc-url "$RPC_URL" \
    --broadcast \
    --legacy \
    --gas-price "$GAS_PRICE" \
    -vv | tee /tmp/fee-collector-deploy.log

FEE_COLLECTOR=$(grep "FeeCollector deployed at:" /tmp/fee-collector-deploy.log | tail -1 | awk '{print $NF}')
if [ -z "$FEE_COLLECTOR" ]; then
    echo -e "${RED}Error: Failed to extract FeeCollector address${NC}"
    exit 1
fi

echo -e "${GREEN}FeeCollector deployed at: $FEE_COLLECTOR${NC}"
echo "FEE_COLLECTOR_ADDRESS=$FEE_COLLECTOR" >> .env
echo ""

# Step 3: Register tokens in TokenRegistry (if they exist)
if [ ! -z "$COMPLIANT_USDT_ADDRESS" ]; then
    echo -e "${YELLOW}Step 3: Registering CompliantUSDT in TokenRegistry...${NC}"
    cast send "$TOKEN_REGISTRY" \
        "registerToken(address,string,string,uint8,bool,address)" \
        "$COMPLIANT_USDT_ADDRESS" \
        "Tether USD (Compliant)" \
        "cUSDT" \
        6 \
        false \
        0x0000000000000000000000000000000000000000 \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        --gas-price "$GAS_PRICE" || echo -e "${YELLOW}Warning: Failed to register CompliantUSDT${NC}"
fi

if [ ! -z "$COMPLIANT_USDC_ADDRESS" ]; then
    echo -e "${YELLOW}Step 4: Registering CompliantUSDC in TokenRegistry...${NC}"
    cast send "$TOKEN_REGISTRY" \
        "registerToken(address,string,string,uint8,bool,address)" \
        "$COMPLIANT_USDC_ADDRESS" \
        "USD Coin (Compliant)" \
        "cUSDC" \
        6 \
        false \
        0x0000000000000000000000000000000000000000 \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        --gas-price "$GAS_PRICE" || echo -e "${YELLOW}Warning: Failed to register CompliantUSDC${NC}"
fi

echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Deployment Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "Deployed Contracts:"
echo "  TokenRegistry: $TOKEN_REGISTRY"
echo "  FeeCollector: $FEE_COLLECTOR"
echo ""
echo "Addresses have been saved to .env file"
