use crate::bitcoin::client::BitcoinClient;
use crate::bitcoin::driver;
use bdk::wallet::AddressIndex;

/// Run the simplest transfer flow. There is no policy file,
/// it's just a normal bitcoin transaction for a sanity check.
pub async fn run() -> Result<(), anyhow::Error> {
    let (mut dave, sammy) = driver::setup().await?;

    let send_amount = 500;
    let _ = ensure_enough_sats(&dave, send_amount);

    let sammy_address = sammy.wallet.get_address(AddressIndex::New)?;
    let tx = dave.build_and_sign_send_tx(sammy_address, send_amount)?;
    dave.broadcast(&tx)?;

    Ok(())
}

/// Exit if the wallet does not have enough sats to send.
fn ensure_enough_sats(wallet: &BitcoinClient, send_amount: u64) -> anyhow::Result<()> {
    if wallet.wallet.get_balance()?.get_total() < send_amount {
        tracing::error!(
            "Please send at least {} sats to the receiving address. Exiting.",
            send_amount
        );
        std::process::exit(0);
    }
    Ok(())
}
