#!/bin/bash

# Send 20M cUSDT and cUSDC to a wallet address
# Usage: ./send-20m-tokens.sh

set -e

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

# Change to script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/.." || exit 1

# Load .env
if [ -f .env ]; then
    set -a
    source .env
    set +a
fi

RPC_URL=${RPC_URL:-${RPC_URL_138:-"http://192.168.11.211:8545"}}

# Token addresses (from .env or defaults)
COMPLIANT_USDT_ADDRESS=${COMPLIANT_USDT_ADDRESS:-"0x93E66202A11B1772E55407B32B44e5Cd8eda7f22"}
COMPLIANT_USDC_ADDRESS=${COMPLIANT_USDC_ADDRESS:-"0xf22258f57794CC8E06237084b353Ab30fFfa640b"}

# Recipient address
RECIPIENT="0x4207aA9aC89B8bF4795dbAbBbE17fdd224E7947C"

# Amount: 20,000,000 tokens with 6 decimals = 20000000000000
AMOUNT="20000000000000"

echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║     Send 20M cUSDT and cUSDC                               ║${NC}"
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}"
echo ""

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

DEPLOYER=$(cast wallet address --private-key "$PRIVATE_KEY" 2>/dev/null)
echo -e "${BLUE}Deployer Address:${NC} $DEPLOYER"
echo -e "${BLUE}Recipient Address:${NC} $RECIPIENT"
echo -e "${BLUE}Amount:${NC} 20,000,000 tokens (each)"
echo ""

# Check balances before transfer
echo -e "${YELLOW}Checking balances...${NC}"

CUSDT_BALANCE=$(cast call "$COMPLIANT_USDT_ADDRESS" "balanceOf(address)" "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
CUSDC_BALANCE=$(cast call "$COMPLIANT_USDC_ADDRESS" "balanceOf(address)" "$DEPLOYER" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

CUSDT_BALANCE_DISPLAY=$(echo "scale=2; $CUSDT_BALANCE / 1000000" | bc 2>/dev/null || echo "0")
CUSDC_BALANCE_DISPLAY=$(echo "scale=2; $CUSDC_BALANCE / 1000000" | bc 2>/dev/null || echo "0")

echo "  Deployer cUSDT Balance: $CUSDT_BALANCE_DISPLAY tokens"
echo "  Deployer cUSDC Balance: $CUSDC_BALANCE_DISPLAY tokens"
echo ""

# Check if we need to mint or transfer
USE_MINT=false
if [ "$(echo "$CUSDT_BALANCE < $AMOUNT" | bc 2>/dev/null || echo 1)" = "1" ]; then
    echo -e "${YELLOW}Insufficient balance. Will mint tokens directly to recipient...${NC}"
    USE_MINT=true
fi

if [ "$USE_MINT" = "true" ]; then
    # Mint cUSDT directly to recipient
    echo -e "${YELLOW}Minting 20M cUSDT to recipient...${NC}"
    TX_OUTPUT=$(cast send "$COMPLIANT_USDT_ADDRESS" \
        "mint(address,uint256)" \
        "$RECIPIENT" \
        "$AMOUNT" \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        --gas-price 20000000000 \
        2>&1)
else
    # Transfer cUSDT
    echo -e "${YELLOW}Transferring 20M cUSDT...${NC}"
    TX_OUTPUT=$(cast send "$COMPLIANT_USDT_ADDRESS" \
        "transfer(address,uint256)" \
        "$RECIPIENT" \
        "$AMOUNT" \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        --gas-price 20000000000 \
        2>&1)
fi

if echo "$TX_OUTPUT" | grep -qE "transactionHash"; then
    TX_HASH=$(echo "$TX_OUTPUT" | grep -oE "transactionHash[[:space:]]+0x[0-9a-fA-F]{64}" | awk '{print $2}')
    if [ "$USE_MINT" = "true" ]; then
        echo -e "${GREEN}✓ cUSDT mint successful${NC}"
    else
        echo -e "${GREEN}✓ cUSDT transfer successful${NC}"
    fi
    echo "  Transaction Hash: $TX_HASH"
else
    echo -e "${RED}✗ cUSDT operation failed${NC}"
    echo "$TX_OUTPUT"
    exit 1
fi

echo ""

# Check if we need to mint cUSDC
USE_MINT_USDC=false
if [ "$(echo "$CUSDC_BALANCE < $AMOUNT" | bc 2>/dev/null || echo 1)" = "1" ]; then
    USE_MINT_USDC=true
fi

if [ "$USE_MINT_USDC" = "true" ]; then
    # Mint cUSDC directly to recipient
    echo -e "${YELLOW}Minting 20M cUSDC to recipient...${NC}"
    TX_OUTPUT=$(cast send "$COMPLIANT_USDC_ADDRESS" \
        "mint(address,uint256)" \
        "$RECIPIENT" \
        "$AMOUNT" \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        --gas-price 20000000000 \
        2>&1)
else
    # Transfer cUSDC
    echo -e "${YELLOW}Transferring 20M cUSDC...${NC}"
    TX_OUTPUT=$(cast send "$COMPLIANT_USDC_ADDRESS" \
        "transfer(address,uint256)" \
        "$RECIPIENT" \
        "$AMOUNT" \
        --rpc-url "$RPC_URL" \
        --private-key "$PRIVATE_KEY" \
        --legacy \
        --gas-price 20000000000 \
        2>&1)
fi

if echo "$TX_OUTPUT" | grep -qE "transactionHash"; then
    TX_HASH=$(echo "$TX_OUTPUT" | grep -oE "transactionHash[[:space:]]+0x[0-9a-fA-F]{64}" | awk '{print $2}')
    if [ "$USE_MINT_USDC" = "true" ]; then
        echo -e "${GREEN}✓ cUSDC mint successful${NC}"
    else
        echo -e "${GREEN}✓ cUSDC transfer successful${NC}"
    fi
    echo "  Transaction Hash: $TX_HASH"
else
    echo -e "${RED}✗ cUSDC operation failed${NC}"
    echo "$TX_OUTPUT"
    exit 1
fi

echo ""

# Verify transfers
echo -e "${YELLOW}Verifying transfers...${NC}"
sleep 3

RECIPIENT_CUSDT=$(cast call "$COMPLIANT_USDT_ADDRESS" "balanceOf(address)" "$RECIPIENT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")
RECIPIENT_CUSDC=$(cast call "$COMPLIANT_USDC_ADDRESS" "balanceOf(address)" "$RECIPIENT" --rpc-url "$RPC_URL" 2>/dev/null || echo "0")

RECIPIENT_CUSDT_DISPLAY=$(echo "scale=2; $RECIPIENT_CUSDT / 1000000" | bc 2>/dev/null || echo "0")
RECIPIENT_CUSDC_DISPLAY=$(echo "scale=2; $RECIPIENT_CUSDC / 1000000" | bc 2>/dev/null || echo "0")

echo "  Recipient cUSDT Balance: $RECIPIENT_CUSDT_DISPLAY tokens"
echo "  Recipient cUSDC Balance: $RECIPIENT_CUSDC_DISPLAY tokens"
echo ""

echo -e "${GREEN}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║     Transfers Complete!                                     ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════════╝${NC}"
