use bdk::{
    bitcoin::Network,
    database::MemoryDatabase,
    keys::{
        bip39::{Language, Mnemonic, WordCount},
        DerivableKey, ExtendedKey, GeneratableKey, GeneratedKey,
    },
    miniscript,
    template::Bip84,
    KeychainKind, Wallet,
};

pub async fn run() -> Result<(), anyhow::Error> {
    // Generate a new mnemonic
    let mnemonic: GeneratedKey<_, miniscript::Segwitv0> =
        Mnemonic::generate((WordCount::Words12, Language::English)).unwrap();
    let mnemonic_words = mnemonic.to_string();
    let mnemonic = Mnemonic::parse(&mnemonic_words).unwrap();

    // Generate the extended key
    let xkey: ExtendedKey = mnemonic.into_extended_key()?;

    // Get private key from the extended key
    let xprv = xkey.into_xprv(Network::Signet).unwrap();

    // Create a BDK wallet using BIP 84 descriptor ("m/84h/1h/0h/0" and "m/84h/1h/0h/1")
    let wallet = Wallet::new(
        Bip84(xprv, KeychainKind::External),
        Some(Bip84(xprv, KeychainKind::Internal)),
        Network::Signet,
        MemoryDatabase::default(),
    )?;

    let address = wallet.get_address(bdk::wallet::AddressIndex::New)?;
    println!("Generated Address: {:?}", address);

    Ok(())
}
