#!/usr/bin/env bash
# Verify Oracle Publisher Authorization
# Checks if account is authorized as transmitter and has sufficient balance

set -euo pipefail

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
VMID=3500
AGGREGATOR="0x99b3511a2d315a497c8112c1fdd8d508d4b1e506"
RPC_URL="https://rpc-http-pub.d-bis.org"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

echo "========================================="
echo "Oracle Publisher Authorization Check"
echo "========================================="
echo ""

# Get account address from .env
log_info "Getting account address from .env..."
ACCOUNT=$(ssh "root@$PROXMOX_HOST" "pct exec $VMID -- bash" << 'EOF'
cd /opt/oracle-publisher
source .env 2>/dev/null || true
if [ -n "${PRIVATE_KEY:-}" ]; then
    source venv/bin/activate 2>/dev/null || true
    python3 << 'PYEOF'
from eth_account import Account
import os
from dotenv import load_dotenv
load_dotenv()
pk = os.getenv('PRIVATE_KEY', '')
if pk:
    account = Account.from_key(pk)
    print(account.address)
PYEOF
fi
EOF
)

if [ -z "$ACCOUNT" ]; then
    log_error "Could not get account address from .env"
    exit 1
fi

log_success "Account: $ACCOUNT"
echo ""

# Check if transmitter
log_info "Checking transmitter authorization..."
IS_TRANSMITTER=$(cast call "$AGGREGATOR" \
    "isTransmitter(address)" \
    "$ACCOUNT" \
    --rpc-url "$RPC_URL" 2>&1 || echo "error")

if [ "$IS_TRANSMITTER" = "0x0000000000000000000000000000000000000000000000000000000000000001" ]; then
    log_success "Account IS authorized as transmitter"
    TRANSMITTER_STATUS="✅ Authorized"
elif [ "$IS_TRANSMITTER" = "0x0000000000000000000000000000000000000000000000000000000000000000" ]; then
    log_error "Account is NOT authorized as transmitter"
    TRANSMITTER_STATUS="❌ Not Authorized"
else
    log_warn "Could not verify transmitter status: $IS_TRANSMITTER"
    TRANSMITTER_STATUS="⚠️  Unknown"
fi
echo ""

# Check balance
log_info "Checking account balance..."
BALANCE_HEX=$(cast balance "$ACCOUNT" --rpc-url "$RPC_URL" 2>&1 || echo "0x0")
BALANCE_INT=$(python3 -c "print(int('$BALANCE_HEX', 16))" 2>/dev/null || echo "0")
BALANCE_ETH=$(python3 -c "print($BALANCE_INT / 10**18)" 2>/dev/null || echo "0")

if (( $(echo "$BALANCE_ETH > 0.001" | bc -l 2>/dev/null || echo "0") )); then
    log_success "Balance: ${BALANCE_ETH} ETH (sufficient)"
    BALANCE_STATUS="✅ Sufficient"
else
    log_warn "Balance: ${BALANCE_ETH} ETH (may be insufficient)"
    BALANCE_STATUS="⚠️  Low"
fi
echo ""

# Check oracle state
log_info "Checking oracle contract state..."
PAUSED=$(cast call "$AGGREGATOR" "paused()" --rpc-url "$RPC_URL" 2>&1 || echo "error")
ADMIN=$(cast call "$AGGREGATOR" "admin()" --rpc-url "$RPC_URL" 2>&1 || echo "error")

if [ "$PAUSED" = "0x0000000000000000000000000000000000000000000000000000000000000001" ]; then
    log_error "Oracle is PAUSED"
    ORACLE_STATUS="❌ Paused"
elif [ "$PAUSED" = "0x0000000000000000000000000000000000000000000000000000000000000000" ]; then
    log_success "Oracle is NOT paused"
    ORACLE_STATUS="✅ Active"
else
    log_warn "Could not check pause status"
    ORACLE_STATUS="⚠️  Unknown"
fi

if [ "$ADMIN" != "error" ] && [ -n "$ADMIN" ]; then
    log_info "Admin: $ADMIN"
fi
echo ""

# Summary
echo "========================================="
echo "Summary"
echo "========================================="
echo ""
echo "Account: $ACCOUNT"
echo "Transmitter: $TRANSMITTER_STATUS"
echo "Balance: $BALANCE_STATUS"
echo "Oracle: $ORACLE_STATUS"
echo ""

# Recommendations
if [ "$TRANSMITTER_STATUS" = "❌ Not Authorized" ]; then
    log_error "ACTION REQUIRED: Account needs to be authorized as transmitter"
    echo ""
    echo "To authorize account:"
    echo "  cast send $AGGREGATOR \\"
    echo "    \"addTransmitter(address)\" \\"
    echo "    \"$ACCOUNT\" \\"
    echo "    --rpc-url $RPC_URL \\"
    echo "    --private-key <ADMIN_PRIVATE_KEY>"
    echo ""
fi

if [ "$BALANCE_STATUS" = "⚠️  Low" ]; then
    log_warn "ACTION RECOMMENDED: Fund account with ETH for gas"
    echo ""
fi

if [ "$ORACLE_STATUS" = "❌ Paused" ]; then
    log_error "ACTION REQUIRED: Unpause oracle contract"
    echo ""
    echo "To unpause:"
    echo "  cast send $AGGREGATOR \\"
    echo "    \"unpause()\" \\"
    echo "    --rpc-url $RPC_URL \\"
    echo "    --private-key <ADMIN_PRIVATE_KEY>"
    echo ""
fi

