/*
 * Enhanced Header Integration Test
 * 
 * Tests that the enhanced nostr_core.h master header provides
 * easy access to all documented functionality
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// Test the enhanced master header - single include for everything
#include "../nostr_core/nostr_core.h"

int main(void) {
    printf("NOSTR Core Library - Enhanced Header Integration Test\n");
    printf("====================================================\n\n");
    
    // Initialize crypto subsystem
    if (nostr_crypto_init() != 0) {
        printf("❌ Failed to initialize crypto subsystem\n");
        return 1;
    }
    
    printf("✅ Successfully included nostr_core.h master header\n");
    printf("✅ Crypto subsystem initialized\n\n");
    
    // Test 1: Basic cryptographic functions are available
    printf("=== Test 1: Basic Crypto Functions ===\n");
    
    unsigned char test_data[] = "Hello, NOSTR!";
    unsigned char hash[32];
    
    if (nostr_sha256(test_data, strlen((char*)test_data), hash) == 0) {
        printf("✅ nostr_sha256() - Single-call SHA-256 works\n");
    } else {
        printf("❌ nostr_sha256() failed\n");
        goto cleanup;
    }
    
    // Test 2: Streaming SHA-256 functions are available
    printf("\n=== Test 2: Streaming SHA-256 Functions ===\n");
    
    nostr_sha256_ctx_t ctx;
    unsigned char streaming_hash[32];
    
    if (nostr_sha256_init(&ctx) == 0 &&
        nostr_sha256_update(&ctx, test_data, strlen((char*)test_data)) == 0 &&
        nostr_sha256_final(&ctx, streaming_hash) == 0) {
        printf("✅ nostr_sha256_init/update/final() - Streaming SHA-256 works\n");
        
        // Verify streaming matches single-call
        if (memcmp(hash, streaming_hash, 32) == 0) {
            printf("✅ Streaming result matches single-call result\n");
        } else {
            printf("❌ Streaming result differs from single-call\n");
        }
    } else {
        printf("❌ Streaming SHA-256 functions failed\n");
        goto cleanup;
    }
    
    // Test 3: Key generation functions are available
    printf("\n=== Test 3: Key Generation Functions ===\n");
    
    unsigned char private_key[32] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
        0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00,
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
    };
    unsigned char public_key[32];
    
    if (nostr_ec_private_key_verify(private_key) == 0) {
        printf("✅ nostr_ec_private_key_verify() - Private key validation works\n");
    } else {
        printf("❌ Private key validation failed\n");
        goto cleanup;
    }
    
    if (nostr_ec_public_key_from_private_key(private_key, public_key) == 0) {
        printf("✅ nostr_ec_public_key_from_private_key() - Public key generation works\n");
    } else {
        printf("❌ Public key generation failed\n");
        goto cleanup;
    }
    
    // Test 4: Event functions are available
    printf("\n=== Test 4: Event Functions ===\n");
    
    cJSON* event = nostr_create_and_sign_event(1, "Test message", NULL, private_key, time(NULL));
    if (event) {
        printf("✅ nostr_create_and_sign_event() - Event creation works\n");
        
        if (nostr_validate_event(event) == 0) {
            printf("✅ nostr_validate_event() - Event validation works\n");
        } else {
            printf("❌ Event validation failed\n");
        }
        
        cJSON_Delete(event);
    } else {
        printf("❌ Event creation failed\n");
        goto cleanup;
    }
    
    // Test 5: Utility functions are available
    printf("\n=== Test 5: Utility Functions ===\n");
    
    char hex_output[65];
    nostr_bytes_to_hex(hash, 32, hex_output);
    printf("✅ nostr_bytes_to_hex() - Hash as hex: %.16s...\n", hex_output);
    
    unsigned char hex_back[32];
    if (nostr_hex_to_bytes(hex_output, hex_back, 32) == 0) {
        printf("✅ nostr_hex_to_bytes() - Hex conversion works\n");
        
        if (memcmp(hash, hex_back, 32) == 0) {
            printf("✅ Round-trip hex conversion successful\n");
        } else {
            printf("❌ Round-trip hex conversion failed\n");
        }
    } else {
        printf("❌ Hex to bytes conversion failed\n");
    }
    
    printf("\n====================================================\n");
    printf("Enhanced Header Integration Test Results:\n");
    printf("✅ Master header includes all functionality\n");
    printf("✅ All documented function categories accessible\n");
    printf("✅ Single #include provides complete API\n");
    printf("✅ Functions work correctly through master header\n");
    printf("\nDevelopers can now easily discover and use all\n");
    printf("NOSTR Core Library functions with just:\n");
    printf("    #include \"nostr_core.h\"\n");
    printf("====================================================\n");
    
cleanup:
    nostr_crypto_cleanup();
    return 0;
}
