// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3

#pragma once

#include <string>
#include <fstream>
#include <functional>
#include <mutex>

namespace dragonx {
namespace util {

/**
 * @brief Simple file logger
 */
class Logger {
public:
    Logger();
    ~Logger();

    /**
     * @brief Initialize logger with output file
     * @param path Path to log file
     * @return true if opened successfully
     */
    bool init(const std::string& path);

    /**
     * @brief Write a log message
     * @param message Message to log
     */
    void write(const std::string& message);

    /**
     * @brief Write a formatted log message
     * @param format printf-style format string
     */
    void writef(const char* format, ...);

    /**
     * @brief Get the singleton instance
     */
    static Logger& instance();

    /**
     * @brief Set a callback invoked for every log message.
     * The callback receives the raw (unformatted) message string.
     * Thread-safe — the callback is invoked under the logger lock.
     */
    void setCallback(std::function<void(const std::string&)> cb);

    /**
     * @brief Enable/disable verbose (diagnostic) logging.
     * When disabled, VERBOSE_LOGF calls are suppressed.
     */
    void setVerbose(bool v) { verbose_ = v; }
    bool isVerbose() const { return verbose_; }

private:
    std::ofstream file_;
    std::mutex mutex_;
    bool initialized_ = false;
    bool verbose_ = false;
    std::function<void(const std::string&)> callback_;
};

// Convenience macros
#define LOG(msg) dragonx::util::Logger::instance().write(msg)
#define LOGF(...) dragonx::util::Logger::instance().writef(__VA_ARGS__)

#ifdef DRAGONX_DEBUG
#define DEBUG_LOG(msg) LOG(msg)
#define DEBUG_LOGF(...) LOGF(__VA_ARGS__)
#else
#define DEBUG_LOG(msg)
#define DEBUG_LOGF(...)
#endif

// Verbose logging — only emitted when Logger::isVerbose() is true.
// Used for detailed diagnostic output (connection attempts, daemon state, etc.)
#define VERBOSE_LOGF(...) do { \
    if (dragonx::util::Logger::instance().isVerbose()) \
        dragonx::util::Logger::instance().writef(__VA_ARGS__); \
} while (0)

} // namespace util
} // namespace dragonx
