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

#ifdef _WIN32
#include "SAPITTSEngine.hpp"
#elif defined(__linux__)
#include "EspeakTTSEngine.hpp"
#endif

namespace celuna {

// Stub TTS for unsupported platforms
class StubTTSEngine : public ITTSEngine {
public:
    void speak(const std::string& text, bool async) override {
        spdlog::info("[TTS Stub] Would speak: {}", text.substr(0, 50));
    }
    void stop() override {}
    void setRate(int rate) override {}
    void setVolume(int volume) override {}
    bool isSpeaking() const override { return false; }
    bool isAvailable() const override { return false; }
    std::string getEngineName() const override { return "stub"; }
};

std::unique_ptr<ITTSEngine> TTSEngineFactory::create() {
    auto logger = spdlog::get("TTSFactory");
    if (!logger) {
        logger = spdlog::stdout_color_mt("TTSFactory");
    }

#ifdef _WIN32
    auto engine = std::make_unique<SAPITTSEngine>();
    if (engine->isAvailable()) {
        logger->info("Using SAPI TTS engine");
        return engine;
    }
#elif defined(__linux__)
    auto engine = std::make_unique<EspeakTTSEngine>();
    if (engine->isAvailable()) {
        logger->info("Using espeak TTS engine");
        return engine;
    }
#endif

    logger->warn("No TTS engine available, using stub");
    return std::make_unique<StubTTSEngine>();
}

} // namespace celuna
