#!/usr/bin/env node
/**
 * CLI to get Monero balance using monero-javascript.
 * Usage: node scripts/get-balance-cli.js <address> <view_key>
 * Or: node scripts/get-balance-cli.js (then enter address and view_key when prompted)
 *
 * Example (test data): node scripts/get-balance-cli.js 48edf... abc123...
 */
'use strict';

const readline = require('readline');
const { getBalanceFromViewKey } = require('../lib/monero-balance');

async function main() {
    let address = process.argv[2];
    let viewKey = process.argv[3];

    if (!address || !viewKey) {
        const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
        address = await new Promise(r => rl.question('Monero address: ', r));
        viewKey = await new Promise(r => rl.question('Private view key: ', r));
        rl.close();
    }

    if (!address || !viewKey) {
        console.error('Usage: node scripts/get-balance-cli.js <address> <view_key>');
        process.exit(1);
    }

    console.log('Syncing wallet (this may take a minute)...');
    try {
        const result = await getBalanceFromViewKey(address, viewKey);
        console.log('Balance (atomic):', result.balance.toString());
        console.log('Unlocked (atomic):', result.unlockedBalance.toString());
        console.log('Balance (XMR):', result.balanceXmr);
        console.log('Unlocked (XMR):', result.unlockedBalanceXmr);
    } catch (err) {
        console.error('Error:', err.message);
        process.exit(1);
    }
}

main();
