#ifndef ANET_IRC_H
#define ANET_IRC_H

#include "common.h"

typedef enum {
    SASL_NONE = 0,      /* no password — skip SASL entirely */
    SASL_WAIT_CAP,      /* sent CAP LS, waiting for server's capability list */
    SASL_WAIT_ACK,      /* sent CAP REQ :sasl, waiting for ACK */
    SASL_WAIT_PLUS,     /* sent AUTHENTICATE PLAIN, waiting for AUTHENTICATE + */
    SASL_WAIT_RESULT,   /* credentials sent, waiting for 903/904 */
    SASL_DONE           /* negotiation complete; NICK/USER have been sent */
} sasl_state_t;

typedef struct {
    char nick[64];
    char text[768];
    time_t ts;
    int status;
} chat_line_t;

typedef struct {
    char server[128];
    int port;
    char nick[64];
    char channel[64];
    int tls;
    int connected;
    int registered;
    int should_quit;
    socket_t sock;
#ifdef _WIN32
    HANDLE thread;
#else
    pthread_t thread;
#endif
    int thread_running;
    char recvbuf[IRC_RECV_BUF];
    size_t recv_used;
    chat_line_t lines[UI_MAX_LINES];
    int line_count;
    char users[UI_MAX_USERS][64];
    int user_count;
    int dirty_chat;
    int dirty_users;
    int dirty_status;
    char password[128];
    sasl_state_t sasl_state;
} irc_client_t;

void irc_init(irc_client_t *c);
int irc_connect_server(irc_client_t *c, const char *server, int port,
                       const char *nick, const char *channel, int tls,
                       const char *password);
void irc_disconnect(irc_client_t *c, const char *reason);
int irc_send_raw(irc_client_t *c, const char *line);
int irc_send_privmsg(irc_client_t *c, const char *target, const char *text);
int irc_handle_command(irc_client_t *c, const char *input);
void irc_add_line(irc_client_t *c, const char *nick, const char *text, int status);

#endif
