// Copyright 2026 Cloudflavor GmbH

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Error types for the oai-sdk crate

use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Ollama API error response format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiErrorResponse {
    /// Error message from the API
    pub error: String,
}

/// Errors that can occur when using the Ollama client
#[derive(Error, Debug)]
pub enum OllamaError {
    /// Error occurred during HTTP request
    #[error("HTTP request failed: {0}")]
    RequestError(#[from] reqwest::Error),

    /// Error occurred during JSON parsing
    #[error("JSON parsing failed: {0}")]
    JsonError(#[from] serde_json::Error),

    /// Error occurred during URL parsing
    #[error("URL parsing failed: {0}")]
    UrlError(#[from] url::ParseError),

    /// Ollama API returned an error response
    #[error("Ollama API error ({status}): {message}")]
    ApiError {
        /// HTTP status code
        status: u16,
        /// Error message from the API
        message: String,
    },

    /// Invalid configuration provided
    #[error("Invalid configuration: {0}")]
    ConfigError(&'static str),

    /// Streaming error
    #[error("Streaming error: {0}")]
    StreamError(&'static str),

    /// Model not found
    #[error("Model not found: {0}")]
    ModelNotFound(String),

    /// Operation not supported
    #[error("Operation not supported: {0}")]
    UnsupportedOperation(&'static str),
}

/// Result type alias for Ollama client operations
pub type Result<T> = std::result::Result<T, OllamaError>;
