/*
 * NOSTR Core - Common Definitions
 * Shared error constants and basic types for the modular NOSTR library
 */

#ifndef NOSTR_COMMON_H
#define NOSTR_COMMON_H

#include <stddef.h>
#include <stdint.h>

// Return codes
#define NOSTR_SUCCESS 0
#define NOSTR_ERROR_INVALID_INPUT -1
#define NOSTR_ERROR_CRYPTO_FAILED -2
#define NOSTR_ERROR_MEMORY_FAILED -3
#define NOSTR_ERROR_IO_FAILED -4
#define NOSTR_ERROR_NETWORK_FAILED -5
#define NOSTR_ERROR_NIP04_INVALID_FORMAT -10
#define NOSTR_ERROR_NIP04_DECRYPT_FAILED -11
#define NOSTR_ERROR_NIP04_BUFFER_TOO_SMALL -12
#define NOSTR_ERROR_NIP44_INVALID_FORMAT -13
#define NOSTR_ERROR_NIP44_DECRYPT_FAILED -14
#define NOSTR_ERROR_NIP44_BUFFER_TOO_SMALL -15
#define NOSTR_ERROR_NIP05_INVALID_IDENTIFIER -16
#define NOSTR_ERROR_NIP05_HTTP_FAILED -17
#define NOSTR_ERROR_NIP05_JSON_PARSE_FAILED -18
#define NOSTR_ERROR_NIP05_NAME_NOT_FOUND -19
#define NOSTR_ERROR_NIP05_PUBKEY_MISMATCH -20


// Constants
#define NOSTR_PRIVATE_KEY_SIZE 32
#define NOSTR_PUBLIC_KEY_SIZE 32
#define NOSTR_HEX_KEY_SIZE 65  // 64 + null terminator
#define NOSTR_BECH32_KEY_SIZE 100
#define NOSTR_MAX_CONTENT_SIZE 2048
#define NOSTR_MAX_URL_SIZE 256
#define NIP05_DEFAULT_TIMEOUT 10

// NIP-04 Constants
#define NOSTR_NIP04_MAX_PLAINTEXT_SIZE 16777216    // 16MB
#define NOSTR_NIP04_MAX_ENCRYPTED_SIZE 22369621    // ~21.3MB (accounts for base64 overhead + IV)

// NIP-44 Constants  
#define NOSTR_NIP44_MAX_PLAINTEXT_SIZE 65536       // 64KB max plaintext (matches crypto header)

// Forward declaration for cJSON (to avoid requiring cJSON.h in header)
struct cJSON;

// Relay query modes
typedef enum {
    RELAY_QUERY_FIRST_RESULT,   // Return as soon as first event is received
    RELAY_QUERY_MOST_RECENT,    // Return the most recent event from all relays
    RELAY_QUERY_ALL_RESULTS     // Return all unique events from all relays
} relay_query_mode_t;

// Publish result types
typedef enum {
    PUBLISH_SUCCESS,    // Event was accepted by relay
    PUBLISH_REJECTED,   // Event was rejected by relay  
    PUBLISH_TIMEOUT,    // No response within timeout
    PUBLISH_ERROR       // Connection or other error
} publish_result_t;

// Progress callback function types
typedef void (*relay_progress_callback_t)(
    const char* relay_url,
    const char* status,
    const char* event_id,
    int events_received,
    int total_relays,
    int completed_relays,
    void* user_data);

typedef void (*publish_progress_callback_t)(
    const char* relay_url,
    const char* status,
    const char* message,
    int success_count,
    int total_relays,
    int completed_relays,
    void* user_data);

// Function declarations
const char* nostr_strerror(int error_code);

// Library initialization functions  
int nostr_init(void);
void nostr_cleanup(void);

// Relay query functions
struct cJSON** synchronous_query_relays_with_progress(
    const char** relay_urls,
    int relay_count,
    struct cJSON* filter,
    relay_query_mode_t mode,
    int* result_count,
    int relay_timeout_seconds,
    relay_progress_callback_t callback,
    void* user_data);

// Relay publish functions  
publish_result_t* synchronous_publish_event_with_progress(
    const char** relay_urls,
    int relay_count,
    struct cJSON* event,
    int* success_count,
    int relay_timeout_seconds,
    publish_progress_callback_t callback,
    void* user_data);

#endif // NOSTR_COMMON_H
