#ifndef FEATURES_H
#define FEATURES_H

#include "irc_client.h"
#include <time.h>
#include <regex.h>

// ============================================================================
// NETWORK MANAGER
// ============================================================================

typedef struct {
    char name[64];
    irc_client_t *client;
    int active;
    int channels_count;
    time_t joined_time;
} network_connection_t;

typedef struct {
    network_connection_t networks[10];
    int network_count;
    int current_network;
} network_manager_t;

network_manager_t *network_manager_create(void);
void network_manager_destroy(network_manager_t *nm);
int network_manager_add(network_manager_t *nm, const char *name, irc_client_t *client);
int network_manager_switch(network_manager_t *nm, int index);
int network_manager_remove(network_manager_t *nm, int index);
void network_manager_list(network_manager_t *nm);
irc_client_t *network_manager_get_current(network_manager_t *nm);

// ============================================================================
// THEME SYSTEM
// ============================================================================

typedef struct {
    char name[32];
    int header_color;
    int text_color;
    int nick_color;
    int status_color;
    int highlight_color;
    int border_color;
    int op_color;
    int voice_color;
} theme_t;

void theme_apply(const theme_t *theme);
const theme_t *theme_get_default(void);
const theme_t *theme_get_dark(void);
const theme_t *theme_get_light(void);
const theme_t *theme_get_neon(void);
const theme_t *theme_get_by_name(const char *name);
void theme_list(void);

// ============================================================================
// AUTO-RECONNECT
// ============================================================================

typedef struct {
    int enabled;
    int max_attempts;
    int attempt_count;
    int delay_seconds;
    time_t last_attempt;
} reconnect_config_t;

void reconnect_init(reconnect_config_t *rc);
int reconnect_should_retry(reconnect_config_t *rc);
void reconnect_increment(reconnect_config_t *rc);
void reconnect_reset(reconnect_config_t *rc);
void reconnect_set_enabled(reconnect_config_t *rc, int enabled);

// ============================================================================
// MESSAGE FILTERING
// ============================================================================

#define FILTER_IGNORE 0
#define FILTER_HIGHLIGHT 1
#define FILTER_LOG 2
#define FILTER_ALERT 3

typedef struct {
    char pattern[256];
    int type;
    char action[256];
    regex_t *regex;
} message_filter_t;

typedef struct {
    message_filter_t filters[50];
    int filter_count;
} filter_manager_t;

filter_manager_t *filter_manager_create(void);
void filter_manager_destroy(filter_manager_t *fm);
int filter_manager_add(filter_manager_t *fm, const char *pattern, int type, const char *action);
int filter_manager_remove(filter_manager_t *fm, int index);
int filter_manager_check(filter_manager_t *fm, const char *text);
void filter_manager_list(filter_manager_t *fm);

// ============================================================================
// ALIASES & TRIGGERS
// ============================================================================

typedef struct {
    char name[64];
    char command[256];
    time_t created;
} alias_entry_t;

typedef struct {
    alias_entry_t aliases[100];
    int alias_count;
} alias_manager_t;

alias_manager_t *alias_manager_create(void);
void alias_manager_destroy(alias_manager_t *am);
int alias_manager_add(alias_manager_t *am, const char *name, const char *command);
int alias_manager_remove(alias_manager_t *am, const char *name);
char *alias_manager_expand(alias_manager_t *am, const char *input, char *output, int max_len);
void alias_manager_list(alias_manager_t *am);

// ============================================================================
// DCC TRANSFERS
// ============================================================================

#define DCC_SEND 0
#define DCC_RECEIVE 1

#define DCC_PENDING 0
#define DCC_ACTIVE 1
#define DCC_COMPLETE 2
#define DCC_FAILED 3

typedef struct {
    char filename[256];
    char sender[64];
    long file_size;
    long bytes_transferred;
    int port;
    int socket;
    FILE *fp;
    int type;
    int status;
    time_t start_time;
} dcc_transfer_t;

typedef struct {
    dcc_transfer_t transfers[20];
    int transfer_count;
} dcc_manager_t;

dcc_manager_t *dcc_manager_create(void);
void dcc_manager_destroy(dcc_manager_t *dm);
int dcc_init_send(dcc_manager_t *dm, const char *filename, const char *nickname);
int dcc_init_receive(dcc_manager_t *dm, const char *filename, const char *sender, int port);
void dcc_cleanup(dcc_transfer_t *dcc);
void dcc_manager_list(dcc_manager_t *dm);
void dcc_manager_remove(dcc_manager_t *dm, int index);

// ============================================================================
// PROXY SUPPORT
// ============================================================================

#define PROXY_NONE 0
#define PROXY_SOCKS5 1
#define PROXY_HTTP 2

typedef struct {
    int enabled;
    int type;
    char host[64];
    int port;
    char username[64];
    char password[64];
} proxy_config_t;

proxy_config_t *proxy_config_create(void);
void proxy_config_destroy(proxy_config_t *pc);
int proxy_config_set_socks5(proxy_config_t *pc, const char *host, int port, const char *username, const char *password);
int proxy_config_set_http(proxy_config_t *pc, const char *host, int port);
void proxy_config_disable(proxy_config_t *pc);

// ============================================================================
// IPv6 SUPPORT
// ============================================================================

#include <netdb.h>

int ipv6_resolve_address(const char *host, struct addrinfo **result);
const char *ipv6_get_address_type(const char *address);
int ipv6_is_ipv6_address(const char *address);

// ============================================================================
// TLS CERTIFICATE VALIDATION
// ============================================================================

typedef struct {
    int verify_enabled;
    int allow_self_signed;
    char pinned_certs[10][128];
    int pinned_cert_count;
    char ca_bundle_path[256];
} cert_validation_t;

cert_validation_t *cert_validation_create(void);
void cert_validation_destroy(cert_validation_t *cv);
int cert_validation_pin_cert(cert_validation_t *cv, const char *cert_fingerprint);
void cert_validation_set_ca_bundle(cert_validation_t *cv, const char *path);
void cert_validation_allow_self_signed(cert_validation_t *cv, int allow);

#endif