#!/bin/bash

echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bash_profile
source $HOME/.bash_profile


# Display options to the user
echo "Select an option:"
echo "1. Create New Wallet"
echo "2. Import Old Wallet"
echo "3. To Continue (Skip if you already have a wallet)"

# Get user input
read OPTION

case $OPTION in
    1)
        echo "Please enter your wallet name:"
        read WALLET
        echo "Creating a new wallet..."
        $HOME/go/bin/initiad keys add $WALLET
        echo "Please Backup the Mnemonic keys above."
        echo ""  # Adds a blank line for better spacing
        echo "export WALLET=$WALLET" >> ~/.bash_profile
        ;;
    2)
        echo "Please enter your wallet name:"
        read WALLET
        echo "Importing an old wallet..."
        $HOME/go/bin/initiad keys add $WALLET --recover
        echo "Please Backup the Mnemonic keys above."
        echo ""  # Adds a blank line for better spacing
        echo "export WALLET=$WALLET" >> ~/.bash_profile
        ;;
    3)
        echo "Skipping wallet setup..."
        ;;
    *)
        echo "Invalid option, exiting..."
        exit 1
        ;;
esac

source ~/.bash_profile  # Source once after update

echo "Use this link to access the faucet and get funds: https://faucet.testnet.initia.xyz/"
echo ""  # Adds a blank line for better spacing

while true; do
    echo "Press 1 to check your balance:"
    echo "Press 2 to create a validator (Note: You can only create a validator if you have a balance):"
    read CHOICE

    case $CHOICE in
        1)
            echo "Checking balance for $WALLET..."
            $HOME/go/bin/initiad q bank balances $WALLET --node https://initia-testnet-rpc.synergynodes.com:443
            ;;
        2)
            echo "Please enter your validator name:"
            read VAL_MONIKER
            echo "Creating a validator..."
            $HOME/go/bin/initiad tx mstaking create-validator \
                --amount=500000uinit \
                --pubkey=$($HOME/go/bin/initiad tendermint show-validator) \
                --moniker=$VAL_MONIKER \
                --chain-id=initiation-1 \
                --commission-rate=0.05 \
                --commission-max-rate=0.10 \
                --commission-max-change-rate=0.01 \
                --from=$WALLET \
                --identity="" \
                --website="" \
                --details="BlackNodes Tutorial" \
                --gas=2000000 --fees=300000uinit \
                --node https://initia-testnet-rpc.synergynodes.com:443 \
                -y
            echo "export VAL_MONIKER=$VAL_MONIKER" >> ~/.bash_profile
            source ~/.bash_profile
            break
            ;;
        *)
            echo "Invalid option, please try again."
            ;;
    esac
done
