#!/bin/bash

# Mint 100 cUSDT and 100 cUSDC to a wallet address
# Usage: ./mint-100-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

# Try to use RPC_URL_138 first (if available), then RPC_URL, then fallback to public endpoint
if [ -n "$RPC_URL_138" ]; then
    RPC_URL="$RPC_URL_138"
elif [ -n "$RPC_URL" ]; then
    # Keep RPC_URL as is
    :
else
    RPC_URL="https://rpc-http-pub.d-bis.org"
fi

# Test RPC connectivity and fallback if needed
if ! curl -s -X POST -H "Content-Type: application/json" \
    --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' \
    --max-time 3 "$RPC_URL" > /dev/null 2>&1; then
    echo -e "${YELLOW}RPC endpoint not accessible, trying alternative...${NC}"
    if [ "$RPC_URL" != "http://192.168.11.211:8545" ]; then
        RPC_URL="http://192.168.11.211:8545"
    else
        RPC_URL="https://rpc-http-pub.d-bis.org"
    fi
fi

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

# Recipient address
RECIPIENT="0xc2D6E6981D1A415967A683D615cf97bA9bC26F0f"

# Amount: 100 tokens with 6 decimals = 100000000
AMOUNT="100000000"

echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║     Mint 100 cUSDT and 100 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} 100 tokens (each)"
echo ""

# Mint cUSDT directly to recipient
echo -e "${YELLOW}Minting 100 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)

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}')
    echo -e "${GREEN}✓ cUSDT mint successful${NC}"
    echo "  Transaction Hash: $TX_HASH"
else
    echo -e "${RED}✗ cUSDT mint failed${NC}"
    echo "$TX_OUTPUT"
    exit 1
fi

echo ""

# Mint cUSDC directly to recipient
echo -e "${YELLOW}Minting 100 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)

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}')
    echo -e "${GREEN}✓ cUSDC mint successful${NC}"
    echo "  Transaction Hash: $TX_HASH"
else
    echo -e "${RED}✗ cUSDC mint failed${NC}"
    echo "$TX_OUTPUT"
    exit 1
fi

echo ""

# Verify mints
echo -e "${YELLOW}Verifying mints...${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}║     Minting Complete!                                       ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════════╝${NC}"
