/*
 * Example: Mnemonic Generation and Key Derivation
 * Demonstrates nostr_generate_mnemonic_and_keys()
 */

#include <stdio.h>
#include <stdlib.h>
#include "nostr_core.h"

int main() {
    printf("=== NOSTR Mnemonic Generation Example ===\n\n");
    
    // Initialize the library
    if (nostr_init() != NOSTR_SUCCESS) {
        fprintf(stderr, "Failed to initialize NOSTR library\n");
        return 1;
    }
    
    // Generate mnemonic and derive keys for account 0
    char mnemonic[256];
    unsigned char private_key[NOSTR_PRIVATE_KEY_SIZE];
    unsigned char public_key[NOSTR_PUBLIC_KEY_SIZE];
    int account = 0;
    
    int result = nostr_generate_mnemonic_and_keys(mnemonic, sizeof(mnemonic), 
                                                 account, private_key, public_key);
    if (result != NOSTR_SUCCESS) {
        fprintf(stderr, "Failed to generate mnemonic and keys: %s\n", nostr_strerror(result));
        nostr_cleanup();
        return 1;
    }
    
    // Convert keys to various formats
    char private_hex[NOSTR_HEX_KEY_SIZE];
    char public_hex[NOSTR_HEX_KEY_SIZE];
    char nsec[NOSTR_BECH32_KEY_SIZE];
    char npub[NOSTR_BECH32_KEY_SIZE];
    
    nostr_bytes_to_hex(private_key, NOSTR_PRIVATE_KEY_SIZE, private_hex);
    nostr_bytes_to_hex(public_key, NOSTR_PUBLIC_KEY_SIZE, public_hex);
    nostr_key_to_bech32(private_key, "nsec", nsec);
    nostr_key_to_bech32(public_key, "npub", npub);
    
    // Display results
    printf("✓ Successfully generated BIP39 mnemonic and derived NOSTR keys\n\n");
    printf("BIP39 Mnemonic: %s\n\n", mnemonic);
    printf("Account: %d\n", account);
    printf("Derivation Path: m/44'/1237'/%d'/0/0 (NIP-06)\n\n", account);
    printf("Private Key (hex): %s\n", private_hex);
    printf("Public Key (hex):  %s\n", public_hex);
    printf("nsec (bech32):     %s\n", nsec);
    printf("npub (bech32):     %s\n", npub);
    
    // Cleanup
    nostr_cleanup();
    
    printf("\n✓ Example completed successfully!\n");
    printf("💡 Save the mnemonic safely - it can restore your NOSTR identity\n");
    return 0;
}
