/*
 * WebSocket SSL Test - Test OpenSSL WebSocket implementation
 * Connect to a NOSTR relay and fetch one type 1 event
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../nostr_core/nostr_core.h"
#include "../cjson/cJSON.h"

// Progress callback to show connection status
static void progress_callback(
    const char* relay_url,
    const char* status,
    const char* event_id,
    int events_received,
    int total_relays,
    int completed_relays,
    void* user_data) 
{
    printf("Progress: %s - %s", relay_url ? relay_url : "Summary", status);
    if (event_id) {
        printf(" (Event: %.12s...)", event_id);
    }
    printf(" [%d/%d events, %d/%d relays]\n", 
           events_received, *(int*)user_data, completed_relays, total_relays);
}

int main() {
    printf("WebSocket SSL Test - Testing OpenSSL WebSocket with NOSTR relay\n");
    printf("================================================================\n");
    
    // Initialize NOSTR library
    if (nostr_init() != NOSTR_SUCCESS) {
        printf("❌ Failed to initialize NOSTR library\n");
        return 1;
    }
    
    printf("✅ NOSTR library initialized\n");
    
    // Setup relay and filter
    const char* relay_urls[] = {"wss://nostr.mom"};
    int relay_count = 1;
    
    // Create filter for type 1 events (text notes), limit to 1 event
    cJSON* filter = cJSON_CreateObject();
    cJSON* kinds = cJSON_CreateArray();
    cJSON_AddItemToArray(kinds, cJSON_CreateNumber(1));
    cJSON_AddItemToObject(filter, "kinds", kinds);
    cJSON_AddItemToObject(filter, "limit", cJSON_CreateNumber(1));
    
    printf("📡 Connecting to %s...\n", relay_urls[0]);
    printf("🔍 Requesting 1 type 1 event (text note)...\n\n");
    
    // Query the relay
    int result_count = 0;
    int expected_events = 1;
    cJSON** events = synchronous_query_relays_with_progress(
        relay_urls,
        relay_count,
        filter,
        RELAY_QUERY_FIRST_RESULT,  // Return as soon as we get the first event
        &result_count,
        10,  // 10 second timeout
        progress_callback,
        &expected_events
    );
    
    // Process results
    if (events && result_count > 0) {
        printf("\n✅ Successfully received %d event(s)!\n", result_count);
        printf("📄 Raw JSON event data:\n");
        printf("========================\n");
        
        for (int i = 0; i < result_count; i++) {
            char* json_string = cJSON_Print(events[i]);
            if (json_string) {
                printf("%s\n\n", json_string);
                free(json_string);
            }
            cJSON_Delete(events[i]);
        }
        free(events);
        
        printf("🎉 WebSocket SSL Test PASSED - OpenSSL WebSocket working correctly!\n");
    } else {
        printf("\n❌ No events received or query failed\n");
        printf("❌ WebSocket SSL Test FAILED\n");
        
        // Cleanup and return error
        cJSON_Delete(filter);
        nostr_cleanup();
        return 1;
    }
    
    // Cleanup
    cJSON_Delete(filter);
    nostr_cleanup();
    
    printf("✅ WebSocket connection and TLS working with OpenSSL\n");
    return 0;
}
