//! OpenAI provider implementation

use crate::config::provider::ProviderConfig;
use crate::error::{SageError, SageResult};
use crate::llm::messages::LlmMessage;
use crate::llm::parsers::ResponseParser;
use crate::llm::provider_types::LlmRequestParams;
use crate::llm::streaming::LlmStream;
use crate::tools::types::ToolSchema;
use reqwest::Client;
use serde_json::{Value, json};
use tracing::instrument;

/// OpenAI provider handler
pub struct OpenAiProvider {
    config: ProviderConfig,
    model_params: LlmRequestParams,
    http_client: Client,
}

impl OpenAiProvider {
    /// Create a new OpenAI provider
    pub fn new(
        config: ProviderConfig,
        model_params: LlmRequestParams,
        http_client: Client,
    ) -> Self {
        Self {
            config,
            model_params,
            http_client,
        }
    }

    /// OpenAI chat completion
    #[instrument(skip(self, messages, tools), level = "debug")]
    pub async fn chat(
        &self,
        messages: &[LlmMessage],
        tools: Option<&[ToolSchema]>,
    ) -> SageResult<crate::llm::messages::LlmResponse> {
        let url = format!("{}/chat/completions", self.config.get_base_url());

        let mut request_body = super::request_builder::build_openai_request_body(
            &self.model_params.model,
            messages,
            tools,
            &self.model_params,
            true,
            false,
        )?;

        // OpenAI-specific parameters
        if let Some(stop) = &self.model_params.stop {
            request_body["stop"] = json!(stop);
        }
        if let Some(tools) = tools {
            if !tools.is_empty() {
                if let Some(parallel) = self.model_params.parallel_tool_calls {
                    request_body["parallel_tool_calls"] = json!(parallel);
                }
            }
        }

        let mut request = self.http_client.post(&url).json(&request_body);

        // Add authentication
        if let Some(api_key) = self.config.get_api_key() {
            request = request.bearer_auth(api_key);
        }

        // Add organization header if provided
        if let Some(org) = self.config.organization() {
            request = request.header("OpenAI-Organization", org);
        }

        let response = request.send().await.map_err(|e| {
            SageError::llm_with_context(
                format!("OpenAI request failed: {}", e),
                "Failed to send HTTP request to OpenAI API",
            )
        })?;

        if !response.status().is_success() {
            return Err(super::error_utils::handle_http_error(response, "OpenAI").await);
        }

        let response_json: Value = response
            .json()
            .await
            .map_err(|e| super::error_utils::handle_parse_error(e, "OpenAI"))?;

        ResponseParser::parse_openai(response_json)
    }

    /// OpenAI streaming chat completion
    pub async fn chat_stream(
        &self,
        messages: &[LlmMessage],
        tools: Option<&[ToolSchema]>,
    ) -> SageResult<LlmStream> {
        let url = format!("{}/chat/completions", self.config.get_base_url());

        let request_body = super::request_builder::build_openai_request_body(
            &self.model_params.model,
            messages,
            tools,
            &self.model_params,
            true,
            true,
        )?;

        let mut request = self.http_client.post(&url).json(&request_body);

        // Add authentication
        if let Some(api_key) = self.config.get_api_key() {
            request = request.bearer_auth(api_key);
        }

        let response = request.send().await.map_err(|e| {
            SageError::llm_with_context(
                format!("OpenAI streaming request failed: {}", e),
                "Failed to send HTTP request to OpenAI streaming API",
            )
        })?;

        if !response.status().is_success() {
            return Err(super::error_utils::handle_stream_http_error(response, "OpenAI").await);
        }

        // Convert response to stream
        let byte_stream = response.bytes_stream();
        Ok(super::openai_stream::openai_sse_stream(byte_stream))
    }
}
