/*
 * Ginxsom Relay Client - Nostr Relay Connection Manager
 * 
 * This module enables Ginxsom to act as a Nostr client, connecting to relays
 * to publish events (Kind 0, Kind 10002) and subscribe to admin commands (Kind 23456).
 */

#ifndef RELAY_CLIENT_H
#define RELAY_CLIENT_H

#include <stddef.h>
#include <time.h>

// Connection states for relay tracking
typedef enum {
    RELAY_STATE_DISCONNECTED = 0,
    RELAY_STATE_CONNECTING = 1,
    RELAY_STATE_CONNECTED = 2,
    RELAY_STATE_ERROR = 3
} relay_state_t;

// Relay connection info (in-memory only)
typedef struct {
    char url[256];
    relay_state_t state;
    int reconnect_attempts;
    time_t last_connect_attempt;
    time_t connected_since;
} relay_info_t;

// Initialize relay client system
// Loads configuration from database and prepares for connections
// Returns: 0 on success, -1 on error
int relay_client_init(const char *db_path);

// Start relay connections
// Connects to all relays specified in kind_10002_tags config
// Publishes Kind 0 and Kind 10002 events after successful connection
// Returns: 0 on success, -1 on error
int relay_client_start(void);

// Stop relay connections and cleanup
// Gracefully disconnects from all relays and stops background thread
void relay_client_stop(void);

// Check if relay client is enabled
// Returns: 1 if enabled, 0 if disabled
int relay_client_is_enabled(void);

// Publish Kind 0 profile event to all connected relays
// Uses kind_0_content from config database
// Returns: 0 on success, -1 on error
int relay_client_publish_kind0(void);

// Publish Kind 10002 relay list event to all connected relays
// Uses kind_10002_tags from config database
// Returns: 0 on success, -1 on error
int relay_client_publish_kind10002(void);

// Send Kind 23457 admin response event
// Encrypts content using NIP-44 and publishes to all connected relays
// Parameters:
//   - recipient_pubkey: Admin's public key (recipient)
//   - response_content: JSON response content to encrypt
// Returns: 0 on success, -1 on error
int relay_client_send_admin_response(const char *recipient_pubkey, const char *response_content);

// Get current relay connection status
// Returns JSON string with relay status (caller must free)
// Format: [{"url": "wss://...", "state": "connected", "connected_since": 1234567890}, ...]
char *relay_client_get_status(void);

// Force reconnection to all relays
// Disconnects and reconnects to all configured relays
// Returns: 0 on success, -1 on error
int relay_client_reconnect(void);

#endif // RELAY_CLIENT_H