#include "addressbook.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

address_book_t *addressbook_create(void) {
    address_book_t *ab = malloc(sizeof(address_book_t));
    if (!ab) return NULL;
    
    ab->entry_count = 0;
    memset(ab->entries, 0, sizeof(ab->entries));
    
    // Load from file if exists
    addressbook_load(ab);
    
    return ab;
}

void addressbook_destroy(address_book_t *ab) {
    if (!ab) return;
    addressbook_save(ab);
    free(ab);
}

int addressbook_add(address_book_t *ab, const char *name, const char *server,
                    int port, const char *nick, const char *channel, int ssl) {
    if (!ab || ab->entry_count >= 50) return -1;
    if (!name || !server || !nick || !channel) return -1;
    
    // Check if name already exists
    for (int i = 0; i < ab->entry_count; i++) {
        if (strcmp(ab->entries[i].name, name) == 0) {
            return -1;  // Duplicate name
        }
    }
    
    strncpy(ab->entries[ab->entry_count].name, name, 63);
    strncpy(ab->entries[ab->entry_count].server, server, 127);
    ab->entries[ab->entry_count].port = port;
    strncpy(ab->entries[ab->entry_count].nickname, nick, 63);
    strncpy(ab->entries[ab->entry_count].channel, channel, 63);
    ab->entries[ab->entry_count].use_ssl = ssl;
    
    ab->entry_count++;
    addressbook_save(ab);
    
    return ab->entry_count - 1;
}

int addressbook_remove(address_book_t *ab, int index) {
    if (!ab || index < 0 || index >= ab->entry_count) return -1;
    
    for (int i = index; i < ab->entry_count - 1; i++) {
        ab->entries[i] = ab->entries[i + 1];
    }
    
    ab->entry_count--;
    addressbook_save(ab);
    
    return 0;
}

int addressbook_get(address_book_t *ab, int index, address_book_entry_t *entry) {
    if (!ab || !entry || index < 0 || index >= ab->entry_count) return -1;
    
    *entry = ab->entries[index];
    return 0;
}

void addressbook_list(address_book_t *ab) {
    if (!ab || ab->entry_count == 0) {
        printf("  No address book entries\n");
        return;
    }
    
    printf("\n  ╔════════════════════════════════════════════════════════════╗\n");
    printf("  ║                    Address Book Entries                    ║\n");
    printf("  ╠════════════════════════════════════════════════════════════╣\n");
    
    for (int i = 0; i < ab->entry_count; i++) {
        printf("  ║ [%2d] %-17s %s:%d\n", i, ab->entries[i].name, 
               ab->entries[i].server, ab->entries[i].port);
        printf("  ║      Nick: %-16s Channel: %s\n", 
               ab->entries[i].nickname, ab->entries[i].channel);
        if (ab->entries[i].use_ssl) {
            printf("  ║      SSL/TLS: Yes\n");
        }
    }
    
    printf("  ╚════════════════════════════════════════════════════════════╝\n");
}

int addressbook_load(address_book_t *ab) {
    if (!ab) return -1;
    
    FILE *fp = fopen(".anet_addressbook", "r");
    if (!fp) return 0;  // File doesn't exist yet
    
    char line[512];
    ab->entry_count = 0;
    
    while (fgets(line, sizeof(line), fp) && ab->entry_count < 50) {
        line[strcspn(line, "\n")] = '\0';
        
        char name[64], server[128], nick[64], channel[64];
        int port, ssl;
        
        if (sscanf(line, "%63[^|]|%127[^|]|%d|%63[^|]|%63[^|]|%d",
                   name, server, &port, nick, channel, &ssl) == 6) {
            strncpy(ab->entries[ab->entry_count].name, name, 63);
            strncpy(ab->entries[ab->entry_count].server, server, 127);
            ab->entries[ab->entry_count].port = port;
            strncpy(ab->entries[ab->entry_count].nickname, nick, 63);
            strncpy(ab->entries[ab->entry_count].channel, channel, 63);
            ab->entries[ab->entry_count].use_ssl = ssl;
            
            ab->entry_count++;
        }
    }
    
    fclose(fp);
    return 0;
}

int addressbook_save(address_book_t *ab) {
    if (!ab) return -1;
    
    FILE *fp = fopen(".anet_addressbook", "w");
    if (!fp) return -1;
    
    for (int i = 0; i < ab->entry_count; i++) {
        fprintf(fp, "%s|%s|%d|%s|%s|%d\n",
                ab->entries[i].name,
                ab->entries[i].server,
                ab->entries[i].port,
                ab->entries[i].nickname,
                ab->entries[i].channel,
                ab->entries[i].use_ssl);
    }
    
    fclose(fp);
    return 0;
}