#include "common.h"
#include "zlog.h"
#include "mylog.h"

#define LOG_DIR        PROJECT_ROOT_DIR "/log"
#define ZLOG_CONF_FILE PROJECT_ROOT_DIR "/zlog.conf"
#define LOG_KEEP_DAYS  30
#define MAX_SIZE_MB    200

static zlog_category_t *s_zlog_c = NULL;
static char s_log_file[256];
static char s_config_content[1024];

int logprject_Init();
int logconf_create();
void check_and_delete_log();
static int create_log_dir_if_not_exist(const char *path);

static void generate_log_filename(void)
{
    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    snprintf(s_log_file, sizeof(s_log_file),
             "%s/log_%04d-%02d-%02d.log",
             LOG_DIR, t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
}

static void log_early(const char *level, const char *format, ...)
{
    char msg[512];
    va_list args;
    va_start(args, format);
    vsnprintf(msg, sizeof(msg), format, args);
    va_end(args);

    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    char ts[32];
    snprintf(ts, sizeof(ts), "%04d-%02d-%02d %02d:%02d:%02d",
             t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
             t->tm_hour, t->tm_min, t->tm_sec);

    fprintf(stderr, "%s [%s] %s\n", ts, level, msg);

    if (s_log_file[0] != '\0') {
        FILE *fp = fopen(s_log_file, "a");
        if (fp) {
            fprintf(fp, "%s [%s] %s\n", ts, level, msg);
            fclose(fp);
        }
    }
}

static void generate_config_content(void)
{
    snprintf(s_config_content, sizeof(s_config_content),
        "[global]\n"
        "strict init = true\n"
        "buffer min = 1024\n"
        "buffer max = 2MB\n"
        "default format = \"%%d(%%Y-%%m-%%d %%H:%%M:%%S).%%ms %%-6V (%%c:%%F:%%L) - %%m%%n\"\n"
        "file perms = 600\n"
        "\n"
        "[formats]\n"
        "millisecond = \"%%d(%%Y-%%m-%%d %%H:%%M:%%S).%%ms [%%V] %%m%%n\"\n"
        "\n"
        "[rules]\n"
        "my_log.*    \"%s\"; millisecond\n",
        s_log_file);
}

int mylog_init()
{
    generate_log_filename();
    create_log_dir_if_not_exist(s_log_file);

    log_early("INFO", "===== 系统启动 =====");
    log_early("INFO", "日志文件: %s", s_log_file);

    if (logconf_create() != 0) {
        log_early("FATAL", "日志配置创建失败");
        return 1;
    }

    check_and_delete_log();

    if (logprject_Init() != 0) {
        log_early("FATAL", "zlog初始化失败");
        return 2;
    }

    my_zlog_info("日志初始化成功: %s", s_log_file);
    return 0;
}

int logconf_create()
{
    generate_config_content();

    FILE *fp = fopen(ZLOG_CONF_FILE, "w");
    if (fp == NULL) {
        log_early("ERROR", "Failed to create zlog.conf");
        return 1;
    }
    fwrite(s_config_content, strlen(s_config_content), 1, fp);
    fclose(fp);

    char chmod_cmd[512];
    snprintf(chmod_cmd, sizeof(chmod_cmd), "sudo chmod +666 %s", ZLOG_CONF_FILE);
    system(chmod_cmd);

    log_early("INFO", "zlog.conf created");
    return 0;
}

static int create_log_dir_if_not_exist(const char *path)
{
    char dir_path[256] = {0};
    const char *p = path;
    char *q = dir_path;

    while (*p != '\0') {
        *q = *p;
        if (*p == '/' && *(p + 1) != '\0') {
            if (access(dir_path, F_OK) != 0) {
                if (mkdir(dir_path, 0755) != 0) {
                    fprintf(stderr, "Failed to create directory: %s\n", dir_path);
                    return -1;
                }
            }
        }
        p++;
        q++;
    }

    return 0;
}

int logprject_Init()
{
    int rc;

    rc = zlog_init(ZLOG_CONF_FILE);
    if (rc) {
        log_early("ERROR", "zlog_init failed");
        return -2;
    }

    s_zlog_c = zlog_get_category("my_log");
    if (!s_zlog_c) {
        log_early("ERROR", "zlog_get_category failed");
        zlog_fini();
        return -3;
    }

    char chmod_log_cmd[512];
    snprintf(chmod_log_cmd, sizeof(chmod_log_cmd), "sudo chmod +666 %s", s_log_file);
    system(chmod_log_cmd);

    return 0;
}

void check_and_delete_log()
{
    DIR *dir = opendir(LOG_DIR);
    if (!dir) return;

    time_t now = time(NULL);
    struct dirent *entry;
    char filepath[512];

    while ((entry = readdir(dir)) != NULL) {
        int year, month, day;
        if (sscanf(entry->d_name, "log_%d-%d-%d.log", &year, &month, &day) == 3) {
            struct tm file_tm = {0};
            file_tm.tm_year = year - 1900;
            file_tm.tm_mon  = month - 1;
            file_tm.tm_mday = day;
            time_t file_time = mktime(&file_tm);

            double diff_days = difftime(now, file_time) / 86400.0;

            snprintf(filepath, sizeof(filepath), "%s/%s", LOG_DIR, entry->d_name);

            if (diff_days > LOG_KEEP_DAYS) {
                log_early("INFO", "删除过期日志: %s (%.0f天前)", entry->d_name, diff_days);
                remove(filepath);
                continue;
            }

            struct stat st;
            if (stat(filepath, &st) == 0) {
                double size_mb = st.st_size / (1024.0 * 1024.0);
                if (size_mb > MAX_SIZE_MB) {
                    log_early("WARN", "删除过大日志: %s (%.1fMB)", entry->d_name, size_mb);
                    remove(filepath);
                }
            }
            continue;
        }

        if (strcmp(entry->d_name, "log.log") == 0) {
            snprintf(filepath, sizeof(filepath), "%s/%s", LOG_DIR, entry->d_name);
            log_early("INFO", "删除旧格式日志: log.log");
            remove(filepath);
        }
    }

    closedir(dir);
}

#ifndef LOG_PRODUCTION
void my_zlog_debug(const char *format, ...) {
    if (s_zlog_c) {
        va_list args;
        va_start(args, format);
        vzlog_debug(s_zlog_c, format, args);
        va_end(args);
    }
}

void my_zlog_info(const char *format, ...) {
    if (s_zlog_c) {
        va_list args;
        va_start(args, format);
        vzlog_info(s_zlog_c, format, args);
        va_end(args);
    }
}
#endif

void my_zlog_notice(const char *format, ...) {
    if (s_zlog_c) {
        va_list args;
        va_start(args, format);
        vzlog_notice(s_zlog_c, format, args);
        va_end(args);
    }
}

void my_zlog_warn(const char *format, ...) {
    if (s_zlog_c) {
        va_list args;
        va_start(args, format);
        vzlog_warn(s_zlog_c, format, args);
        va_end(args);
    }
}

void my_zlog_error(const char *format, ...) {
    if (s_zlog_c) {
        va_list args;
        va_start(args, format);
        vzlog_error(s_zlog_c, format, args);
        va_end(args);
    }
}

void my_zlog_fatal(const char *format, ...) {
    if (s_zlog_c) {
        va_list args;
        va_start(args, format);
        vzlog_fatal(s_zlog_c, format, args);
        va_end(args);
    }
}

void my_log_close() {
    if (s_zlog_c) {
        zlog_fini();
        s_zlog_c = NULL;
    }
}
