#ifndef OPEN_WING_CONFIG_H
#define OPEN_WING_CONFIG_H

#include <stddef.h>

#define OW_MAX_NAME_LEN 128
#define OW_MAX_ABOUT_LEN 512
#define OW_MAX_URL_LEN 256
#define OW_MAX_KEY_LEN 256
#define OW_MAX_MODEL_LEN 128

#define OW_MAX_STRANGER_RESPONSE_LEN 512

typedef struct {
    char nsec[OW_MAX_KEY_LEN];
    unsigned char private_key[32];
    unsigned char public_key[32];
    char public_key_hex[65];
} agent_keys_t;

typedef enum {
    DM_PROTOCOL_NIP04 = 0,
    DM_PROTOCOL_NIP17 = 1,
    DM_PROTOCOL_BOTH = 2
} dm_protocol_t;

typedef struct {
    char pubkey[65];
} admin_config_t;

typedef struct {
    char provider[32];
    char api_key[OW_MAX_KEY_LEN];
    char model[OW_MAX_MODEL_LEN];
    char base_url[OW_MAX_URL_LEN];
    int max_tokens;
    double temperature;
} llm_config_t;

typedef struct {
    int enabled;
    int timeout_seconds;
    int max_output_bytes;
    char working_directory[OW_MAX_URL_LEN];
} shell_tools_config_t;

typedef struct {
    int enabled;
    int max_turns;
    int trigger_max_turns;
    int api_default_max_turns;
    int api_max_turns_ceiling;
    int stall_repeat_threshold;
    int local_http_fetch_default_timeout_seconds;
    int local_http_fetch_max_timeout_seconds;
    shell_tools_config_t shell;
    int blossom_max_upload_bytes;
    int blossom_max_download_bytes;
} tools_config_t;

typedef struct {
    int kind;
    char* content;
    char* tags_json;  // JSON array string for tags, optional
} startup_event_t;

typedef struct {
    int enabled;
    int tools_enabled;
} security_tier_config_t;

typedef struct {
    int verify_signatures;
    char stranger_response[OW_MAX_STRANGER_RESPONSE_LEN];
    security_tier_config_t admin;
    security_tier_config_t wot;
    security_tier_config_t stranger;
} security_config_t;

typedef struct {
    int enabled;
    int track_kind_0;
    int track_kind_3;
    int track_kind_10002;
    int track_kind_1;
    int kind_1_limit;
} admin_context_config_t;

typedef struct {
    int enabled;
    int max_active;
    int cooldown_seconds;
    int llm_rate_limit_per_minute;
    int template_rate_limit_per_minute;
} triggers_config_t;

typedef struct {
    int enabled;
    int port;
    char bind_address[OW_MAX_URL_LEN];
} api_config_t;

typedef struct {
    int enabled;
    char** mint_urls;
    int mint_count;
    char unit[16];
    int auto_load;
    int mint_timeout_seconds;
} cashu_wallet_config_t;

typedef struct {
    agent_keys_t keys;
    admin_config_t admin;
    dm_protocol_t dm_protocol;
    char** relays;
    int relay_count;
    llm_config_t llm;
    tools_config_t tools;
    security_config_t security;
    admin_context_config_t admin_context;
    triggers_config_t triggers;
    api_config_t api;
    cashu_wallet_config_t cashu_wallet;
    startup_event_t* startup_events;
    int startup_event_count;
    char config_path[OW_MAX_URL_LEN];
} didactyl_config_t;

int config_load(const char* path, didactyl_config_t* config);
int config_ensure_startup_skill_adoption(didactyl_config_t* config);
const char* config_last_error(void);
void config_free(didactyl_config_t* config);

/* Strip JSONC single-line and block comments, returning malloc'd pure JSON. */
char* jsonc_strip_comments(const char* src, size_t src_len);

#endif