import csv
from pathlib import Path

from libcrypto import Wallet


def create_wallet_row(index: int) -> dict:
    wallet = Wallet.generate()

    return {
        "index": index,
        "private_key_hex": wallet.private_key.hex,
        "wif_compressed": wallet.private_key.to_wif(compressed=True),
        "bitcoin_p2pkh": wallet.get_address("bitcoin", "p2pkh"),
        "bitcoin_p2wpkh": wallet.get_address("bitcoin", "p2wpkh"),
        "bitcoin_p2tr": wallet.get_address("bitcoin", "p2tr"),
        "ethereum": wallet.get_address("ethereum"),
        "tron": wallet.get_address("tron"),
        "tron_hex": wallet.get_address("tron", "hex"),
    }


def main() -> None:
    output_path = Path("generated_wallets.csv")
    total_wallets = 10

    fieldnames = [
        "index",
        "private_key_hex",
        "wif_compressed",
        "bitcoin_p2pkh",
        "bitcoin_p2wpkh",
        "bitcoin_p2tr",
        "ethereum",
        "tron",
        "tron_hex",
    ]

    with output_path.open("w", newline="", encoding="utf-8") as file:
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        writer.writeheader()

        for index in range(1, total_wallets + 1):
            writer.writerow(create_wallet_row(index))

    print(f"Generated {total_wallets} wallets.")
    print(f"Output file: {output_path.resolve()}")
    print("Keep this file offline. It contains private keys.")


if __name__ == "__main__":
    main()