#pragma once
// ── CS2 External C++ WebRadar — Logger ───────────────────────────────────────
// Simple thread-safe console logger with [WebRadar] prefix and colored output.

#include <cstdio>
#include <mutex>
#include <string>
#include <string_view>
#include <chrono>
#include <format>

// Prevent Windows #define ERROR from corrupting our enum Level
#ifdef ERROR
#undef ERROR
#endif

namespace logging {

inline std::mutex g_log_mutex;

enum Level {
    LV_INFO,
    LV_WARN,
    LV_ERROR,
};

inline const char* level_color(Level lv) {
    switch (lv) {
        case LV_INFO:  return "\033[36m";  // cyan
        case LV_WARN:  return "\033[33m";  // yellow
        case LV_ERROR: return "\033[31m";  // red
        default:    return "\033[0m";
    }
}

inline const char* level_label(Level lv) {
    switch (lv) {
        case LV_INFO:  return "INFO";
        case LV_WARN:  return "WARN";
        case LV_ERROR: return "ERROR";
        default:    return "?";
    }
}

template<typename... Args>
void print(Level lv, std::string_view fmt, Args&&... args) {
    std::lock_guard lock(g_log_mutex);

    auto now = std::chrono::system_clock::now();
    auto tt = std::chrono::system_clock::to_time_t(now);
    auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
        now.time_since_epoch()) % 1000;

    std::tm tm;
    localtime_s(&tm, &tt);

    // Write timestamp + level + message
    printf("%s[WebRadar] %02d:%02d:%02d.%03lld [%s] ",
           level_color(lv),
           tm.tm_hour, tm.tm_min, tm.tm_sec,
           static_cast<long long>(ms.count()),
           level_label(lv));

    printf(fmt.data(), std::forward<Args>(args)...);
    printf("\033[0m\n");
    fflush(stdout);
}

template<typename... Args>
void info(std::string_view fmt, Args&&... args) {
    print(LV_INFO, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void warn(std::string_view fmt, Args&&... args) {
    print(LV_WARN, fmt, std::forward<Args>(args)...);
}

template<typename... Args>
void error(std::string_view fmt, Args&&... args) {
    print(LV_ERROR, fmt, std::forward<Args>(args)...);
}

} // namespace logging
