/*
 * Example: Input Type Detection
 * Demonstrates nostr_detect_input_type() and nostr_decode_nsec()
 */

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

int main() {
    printf("=== NOSTR Input Type Detection Example ===\n\n");
    
    // Initialize the library
    if (nostr_init() != NOSTR_SUCCESS) {
        fprintf(stderr, "Failed to initialize NOSTR library\n");
        return 1;
    }
    
    // Test various input types
    const char* test_inputs[] = {
        // Mnemonic
        "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
        
        // Hex nsec (64 chars)
        "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb",
        
        // Bech32 nsec
        "nsec1tkks0lxyf9x2jetlzluxsqqwudlnwvef9xx2muxvt7tsh7lgmrhqvfmaqg",
        
        // Invalid input
        "not-a-valid-input",
        
        // Empty string
        ""
    };
    
    const char* type_names[] = {
        "UNKNOWN",
        "MNEMONIC", 
        "HEX_NSEC",
        "BECH32_NSEC"
    };
    
    int num_tests = sizeof(test_inputs) / sizeof(test_inputs[0]);
    
    for (int i = 0; i < num_tests; i++) {
        printf("Test %d:\n", i + 1);
        printf("Input: \"%s\"\n", test_inputs[i]);
        
        // Detect input type
        nostr_input_type_t type = nostr_detect_input_type(test_inputs[i]);
        printf("Detected Type: %s\n", type_names[type]);
        
        // Try to decode if it's an nsec
        if (type == NOSTR_INPUT_NSEC_HEX || type == NOSTR_INPUT_NSEC_BECH32) {
            unsigned char private_key[NOSTR_PRIVATE_KEY_SIZE];
            int result = nostr_decode_nsec(test_inputs[i], private_key);
            
            if (result == NOSTR_SUCCESS) {
                // Convert back to hex and bech32 to verify
                char private_hex[NOSTR_HEX_KEY_SIZE];
                char nsec_bech32[NOSTR_BECH32_KEY_SIZE];
                
                nostr_bytes_to_hex(private_key, NOSTR_PRIVATE_KEY_SIZE, private_hex);
                nostr_key_to_bech32(private_key, "nsec", nsec_bech32);
                
                printf("✓ Successfully decoded nsec!\n");
                printf("  Hex format:    %s\n", private_hex);
                printf("  Bech32 format: %s\n", nsec_bech32);
            } else {
                printf("✗ Failed to decode nsec: %s\n", nostr_strerror(result));
            }
        }
        
        printf("\n");
    }
    
    // Cleanup
    nostr_cleanup();
    
    printf("✓ Example completed successfully!\n");
    printf("💡 Input detection helps handle different key formats automatically\n");
    return 0;
}
