#pragma once

#include "ISTTEngine.hpp"
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>
#include <memory>
#include <string>
#include <vector>
#include <fstream>

// Forward declarations to avoid hard dependency on Vosk
// Actual Vosk headers will be included in the implementation
struct VoskModel;
struct VoskRecognizer;

namespace celuna {

/**
 * @brief Vosk Speech Recognition Engine
 *
 * Lightweight local STT using Vosk models.
 * Recommended model: vosk-model-small-fr-0.22 (~50MB)
 *
 * Installation:
 * - sudo apt install libvosk-dev
 * - Download model: https://alphacephei.com/vosk/models/vosk-model-small-fr-0.22.zip
 *
 * Features:
 * - Local processing (no API costs)
 * - Real-time transcription
 * - Supports French, English, and 20+ languages
 */
class VoskSTTEngine : public ISTTEngine {
public:
    /**
     * @brief Construct Vosk STT engine
     * @param modelPath Path to Vosk model directory (e.g., "./models/vosk-model-small-fr-0.22")
     * @param sampleRate Audio sample rate (default: 16000 Hz)
     */
    explicit VoskSTTEngine(const std::string& modelPath, float sampleRate = 16000.0f);

    ~VoskSTTEngine() override;

    // Disable copy
    VoskSTTEngine(const VoskSTTEngine&) = delete;
    VoskSTTEngine& operator=(const VoskSTTEngine&) = delete;

    std::string transcribe(const std::vector<float>& audioData) override;
    std::string transcribeFile(const std::string& filePath) override;
    void setLanguage(const std::string& language) override;
    bool isAvailable() const override { return m_available; }
    std::string getEngineName() const override { return "vosk"; }

private:
    VoskModel* m_model = nullptr;
    VoskRecognizer* m_recognizer = nullptr;
    bool m_available = false;
    float m_sampleRate = 16000.0f;
    std::shared_ptr<spdlog::logger> m_logger;

    /**
     * @brief Parse Vosk JSON result
     * @param json JSON string from Vosk (e.g., {"text": "bonjour"})
     * @return Extracted text
     */
    std::string parseVoskResult(const std::string& json);

    /**
     * @brief Read WAV file and extract PCM data
     * @param filePath Path to WAV file
     * @param outSamples Output PCM samples (float)
     * @return true if successful
     */
    bool readWavFile(const std::string& filePath, std::vector<float>& outSamples);
};

} // namespace celuna
