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

#pragma once

#include <string>

namespace dragonx {
namespace util {

/**
 * @brief Ensures only one instance of the wallet runs at a time
 * 
 * Uses file locking on Linux/macOS and named mutex on Windows.
 */
class SingleInstance {
public:
    SingleInstance(const std::string& app_name = "dragonx-wallet");
    ~SingleInstance();
    
    // Non-copyable
    SingleInstance(const SingleInstance&) = delete;
    SingleInstance& operator=(const SingleInstance&) = delete;
    
    /**
     * @brief Try to acquire the single instance lock
     * @return true if lock acquired (this is the only instance)
     */
    bool tryLock();
    
    /**
     * @brief Release the lock
     */
    void unlock();
    
    /**
     * @brief Check if lock is currently held
     */
    bool isLocked() const { return locked_; }
    
private:
    std::string app_name_;
    bool locked_ = false;
    
#ifdef _WIN32
    void* mutex_handle_ = nullptr;
#else
    int lock_fd_ = -1;
    std::string lock_path_;
#endif
};

} // namespace util
} // namespace dragonx
