#pragma once
#include <memory>
#include <string>

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>   // needed before spdlog on MSVC to avoid winsock conflicts

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>

// -------------------------------------------------------------------------
// AppLogger
// Call AppLogger::Init() once from main() before anything else.
// Then use spdlog::get("dayz") or the DAYZ_LOG_* macros everywhere.
// -------------------------------------------------------------------------

namespace AppLogger {

inline void Init(spdlog::level::level_enum level = spdlog::level::info) {
    auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
    console_sink->set_level(level);
    console_sink->set_pattern("[%H:%M:%S.%e] [%^%l%$] [%n] %v");

    auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/dayz-memory.log", /*truncate=*/true);
    file_sink->set_level(spdlog::level::trace);
    file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] %v");

    auto logger = std::make_shared<spdlog::logger>(
        "dayz",
        spdlog::sinks_init_list{console_sink, file_sink}
    );
    logger->set_level(spdlog::level::trace);
    logger->flush_on(spdlog::level::warn);

    spdlog::register_logger(logger);
    spdlog::set_default_logger(logger);
}

inline std::shared_ptr<spdlog::logger> Get() {
    return spdlog::get("dayz");
}

} // namespace AppLogger
