#include "IWindowTracker.hpp"
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#ifdef _WIN32
#include "Win32WindowTracker.hpp"
#elif defined(__linux__)
#include "X11WindowTracker.hpp"
#endif

namespace celuna {

// Stub implementation for unsupported platforms
class StubWindowTracker : public IWindowTracker {
public:
    std::string getCurrentAppName() override { return "unknown"; }
    std::string getCurrentWindowTitle() override { return ""; }
    bool isUserIdle(int thresholdSeconds) override { return false; }
    int getIdleTimeSeconds() override { return 0; }
    bool isAvailable() const override { return false; }
    std::string getPlatformName() const override { return "stub"; }
};

std::unique_ptr<IWindowTracker> WindowTrackerFactory::create() {
    auto logger = spdlog::get("WindowTracker");
    if (!logger) {
        logger = spdlog::stdout_color_mt("WindowTracker");
    }

#ifdef _WIN32
    auto tracker = std::make_unique<Win32WindowTracker>();
    if (tracker->isAvailable()) {
        logger->info("Using Win32 window tracker");
        return tracker;
    }
#elif defined(__linux__)
    auto tracker = std::make_unique<X11WindowTracker>();
    if (tracker->isAvailable()) {
        logger->info("Using X11 window tracker");
        return tracker;
    }
#endif

    logger->warn("No window tracker available, using stub");
    return std::make_unique<StubWindowTracker>();
}

} // namespace celuna
