#pragma once

#include "ILLMProvider.hpp"
#include "../http/HttpClient.hpp"
#include <spdlog/spdlog.h>
#include <memory>

namespace celuna {

/**
 * @brief OpenAI GPT API provider
 *
 * Supports GPT models with function calling.
 * Response format uses tool_calls array.
 */
class OpenAIProvider : public ILLMProvider {
public:
    explicit OpenAIProvider(const json& config);
    ~OpenAIProvider() override = default;

    LLMResponse chat(const std::string& systemPrompt,
                     const json& messages,
                     const json& tools) override;

    json formatToolResults(const std::vector<ToolResult>& results) override;

    void appendAssistantMessage(json& messages, const LLMResponse& response) override;

    std::string getProviderName() const override { return "openai"; }
    void setModel(const std::string& model) override { m_model = model; }
    void setMaxTokens(int maxTokens) override { m_maxTokens = maxTokens; }
    std::string getModel() const override { return m_model; }

private:
    std::string m_apiKey;
    std::string m_model = "gpt-4o";
    std::string m_baseUrl = "https://api.openai.com";
    int m_maxTokens = 4096;

    std::unique_ptr<HttpClient> m_client;
    std::shared_ptr<spdlog::logger> m_logger;

    // Convert tools to OpenAI format
    json convertTools(const json& tools);

    // Parse OpenAI response
    LLMResponse parseResponse(const json& response);

    // Store raw response for appendAssistantMessage
    json m_lastRawResponse;
};

} // namespace celuna
