/*
 * Example demonstrating modular NIP usage
 * Shows how different NIPs can be used independently
 */

#include "nostr_core/nip001.h"  // Basic protocol
#include "nostr_core/nip005.h"  // NIP-05 DNS verification
#include "nostr_core/nip006.h"  // Key derivation
#include "nostr_core/nip011.h"  // Relay information
#include "nostr_core/nip013.h"  // Proof of work
#include "nostr_core/nip019.h"  // Bech32 encoding
#include "nostr_core/utils.h"   // Utility functions
#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("=== Modular NOSTR Core Library Demo ===\n\n");
    
    // Initialize the library
    if (nostr_init() != NOSTR_SUCCESS) {
        printf("Failed to initialize NOSTR library\n");
        return 1;
    }
    
    // Test NIP-006: Key generation and detection
    printf("1. NIP-006: Key Generation and Input Detection\n");
    unsigned char private_key[32], public_key[32];
    
    if (nostr_generate_keypair(private_key, public_key) == NOSTR_SUCCESS) {
        char private_hex[65], public_hex[65];
        nostr_bytes_to_hex(private_key, 32, private_hex);
        nostr_bytes_to_hex(public_key, 32, public_hex);
        
        printf("   Generated keypair:\n");
        printf("   Private: %s\n", private_hex);
        printf("   Public:  %s\n", public_hex);
        
        // Test input type detection
        nostr_input_type_t type = nostr_detect_input_type(private_hex);
        printf("   Input type: %s\n", 
               type == NOSTR_INPUT_NSEC_HEX ? "NSEC_HEX" : 
               type == NOSTR_INPUT_NSEC_BECH32 ? "NSEC_BECH32" : 
               type == NOSTR_INPUT_MNEMONIC ? "MNEMONIC" : "UNKNOWN");
    }
    
    // Test NIP-019: Bech32 encoding
    printf("\n2. NIP-019: Bech32 Encoding\n");
    char bech32_nsec[200], bech32_npub[200];
    
    if (nostr_key_to_bech32(private_key, "nsec", bech32_nsec) == NOSTR_SUCCESS) {
        printf("   nsec: %s\n", bech32_nsec);
    }
    
    if (nostr_key_to_bech32(public_key, "npub", bech32_npub) == NOSTR_SUCCESS) {
        printf("   npub: %s\n", bech32_npub);
    }
    
    // Test NIP-001: Event creation
    printf("\n3. NIP-001: Event Creation\n");
    cJSON* event = nostr_create_and_sign_event(1, "Hello from modular NOSTR!", NULL, private_key, 0);
    if (event) {
        char* event_json = cJSON_Print(event);
        printf("   Created event: %s\n", event_json);
        free(event_json);
        cJSON_Delete(event);
    }
    
    // Test NIP-013: Proof of Work (light test)
    printf("\n4. NIP-013: Proof of Work\n");
    cJSON* pow_event = nostr_create_and_sign_event(1, "PoW test message", NULL, private_key, 0);
    if (pow_event) {
        printf("   Adding PoW (target difficulty: 4)...\n");
        int result = nostr_add_proof_of_work(pow_event, private_key, 4, 100000, 10000, 5000, NULL, NULL);
        if (result == NOSTR_SUCCESS) {
            cJSON* id_item = cJSON_GetObjectItem(pow_event, "id");
            if (id_item) {
                printf("   PoW success! Event ID: %s\n", cJSON_GetStringValue(id_item));
            }
        } else {
            printf("   PoW failed or timeout\n");
        }
        cJSON_Delete(pow_event);
    }
    
    // Test NIP-005: DNS verification (parse only - no network call in example)
    printf("\n5. NIP-005: DNS Identifier Parsing\n");
    const char* test_json = "{\"names\":{\"test\":\"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\"}}";
    char found_pubkey[65];
    
    int result = nostr_nip05_parse_well_known(test_json, "test", found_pubkey, NULL, NULL);
    if (result == NOSTR_SUCCESS) {
        printf("   Parsed pubkey from test JSON: %s\n", found_pubkey);
    }
    
    // Test NIP-011: Just show the structure exists
    printf("\n6. NIP-011: Relay Information Structure\n");
    printf("   Relay info structure available for fetching relay metadata\n");
    printf("   (Network calls not performed in this example)\n");
    
    printf("\n=== All modular NIPs working! ===\n");
    
    nostr_cleanup();
    return 0;
}
