#pragma once

#include "../wrapped_server.h"
#include "../server_capabilities.h"
#include "backend_utils.h"
#include <string>
#include <filesystem>

namespace lemon {
namespace backends {

class WhisperServer : public WrappedServer, public ITranscriptionServer {
public:
    static InstallParams get_install_params(const std::string& backend, const std::string& version);

    inline static const BackendSpec SPEC = BackendSpec(
        "whispercpp",
#ifdef _WIN32
        "whisper-server.exe"
#else
        "whisper-server"
#endif
        , get_install_params
    );

    explicit WhisperServer(const std::string& log_level,
                          ModelManager* model_manager,
                          BackendManager* backend_manager);

    ~WhisperServer() override;

    void load(const std::string& model_name,
             const ModelInfo& model_info,
             const RecipeOptions& options,
             bool do_not_upgrade = false) override;

    void unload() override;

    // ICompletionServer implementation (not supported - return errors)
    json chat_completion(const json& request) override;
    json completion(const json& request) override;
    json responses(const json& request) override;

    // ITranscriptionServer implementation
    json audio_transcriptions(const json& request) override;

private:
    // NPU compiled cache handling
    void download_npu_compiled_cache(const std::string& model_path,
                                      const ModelInfo& model_info,
                                      bool do_not_upgrade);

    // Audio file handling
    std::string save_audio_to_temp(const std::string& audio_data,
                                    const std::string& filename);
    void cleanup_temp_file(const std::string& path);
    void validate_audio_file(const std::string& path);

    // Build request for whisper-server
    json build_transcription_request(const json& request, bool translate = false);

    // Forward audio file using multipart form-data
    json forward_multipart_audio_request(const std::string& file_path,
                                         const json& params,
                                         bool translate);

    // Forward audio data directly (no file I/O) using multipart form-data
    json forward_multipart_audio_data(const std::string& audio_data,
                                      const std::string& filename,
                                      const json& params,
                                      bool translate);

    std::string model_path_;
    std::filesystem::path temp_dir_;  // Directory for temporary audio files
};

} // namespace backends
} // namespace lemon
