#ifndef OPEN_WING_NOSTR_HANDLER_H
#define OPEN_WING_NOSTR_HANDLER_H

#include "config.h"
#include "cjson/cJSON.h"
#include "../nostr_core_lib/nostr_core/nostr_core.h"

struct trigger_manager;

typedef enum {
    DIDACTYL_SENDER_ADMIN = 1,
    DIDACTYL_SENDER_WOT = 2,
    DIDACTYL_SENDER_STRANGER = 3
} didactyl_sender_tier_t;

typedef void (*dm_callback_t)(const char* sender_pubkey_hex,
                              const char* message,
                              didactyl_sender_tier_t tier,
                              void* user_data);

typedef struct {
    int success;
    int kind;
    int relay_count;
    int accepted_by_pool_count;
    char event_id[65];
    char note_uri[256];
    char naddr_uri[1024];
    char d_tag[128];
    char** relays;
} nostr_publish_result_t;

typedef void (*nostr_self_skill_eose_cb_t)(int event_count, void* user_data);

int nostr_handler_init(didactyl_config_t* config);
void nostr_handler_set_trigger_manager(struct trigger_manager* trigger_manager);
int nostr_handler_subscribe_admin_context(void);
int nostr_handler_subscribe_agent_context(void);
int nostr_handler_subscribe_self_skills(void);
char* nostr_handler_get_self_events_by_kind_json(int kind);
void nostr_handler_set_self_skill_eose_callback(nostr_self_skill_eose_cb_t callback, void* user_data);
int nostr_handler_subscribe_dms(dm_callback_t callback, void* user_data);
int nostr_handler_subscribe_wallet_events(void);
int nostr_handler_send_dm(const char* recipient_pubkey_hex, const char* message);
int nostr_handler_send_dm_auto(const char* recipient_pubkey_hex, const char* message);
int nostr_handler_publish_kind_event(int kind, const char* content, cJSON* tags, nostr_publish_result_t* out_result);
void nostr_handler_publish_result_free(nostr_publish_result_t* result);
char* nostr_handler_query_json(cJSON* filter, int timeout_ms);
nostr_pool_subscription_t* nostr_handler_subscribe_with_filter(
    cJSON* filter,
    void (*on_event)(cJSON* event, const char* relay_url, void* user_data),
    void (*on_eose)(cJSON** events, int event_count, void* user_data),
    void* user_data,
    int close_on_eose,
    int enable_deduplication,
    nostr_pool_eose_result_mode_t result_mode,
    int relay_timeout_seconds,
    int eose_timeout_seconds);
int nostr_handler_close_subscription(nostr_pool_subscription_t* subscription);
int nostr_handler_poll(int timeout_ms);
int nostr_handler_reconcile_startup_events(void);
int nostr_handler_load_system_context_from_adopted_skills(void);
void nostr_handler_refresh_relay_statuses(void);
int nostr_handler_sync_relays_from_config(void);
const char* nostr_handler_get_system_context(void);
char* nostr_handler_get_self_skill_events_json(void);
int nostr_handler_is_event_in_self_cache(const char* event_id_hex);
const char* nostr_handler_get_startup_display_name(void);
int nostr_handler_connected_relay_count(void);
char* nostr_handler_get_admin_kind0_context(void);
char* nostr_handler_get_admin_kind3_context(void);
char* nostr_handler_get_admin_kind10002_context(void);
char* nostr_handler_get_admin_kind1_notes_context(void);
char* nostr_handler_get_agent_kind0_context(void);
char* nostr_handler_get_agent_kind3_context(void);
char* nostr_handler_get_agent_kind10002_context(void);
char* nostr_handler_get_agent_kind1_notes_context(void);
int nostr_handler_is_wot_contact(const char* pubkey_hex);
char* nostr_handler_relay_status_json(void);
char* nostr_handler_relay_info_json(const char* relay_url);
char* nostr_handler_subscription_status_json(void);
int nostr_handler_subscription_set_json(const char* name, cJSON* filter, int enabled, int enabled_set);
int nostr_handler_send_dm_nip17(const char* recipient_pubkey_hex, const char* message, const char* subject);
char* nostr_handler_get_dm_history_json(const char* peer_pubkey_hex, int limit);
void nostr_handler_cleanup(void);

#endif