//! HTTP client utilities

use reqwest::{Client, ClientBuilder};
use std::time::Duration;

pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
pub const SHORT_TIMEOUT: Duration = Duration::from_secs(5);
pub const LONG_TIMEOUT: Duration = Duration::from_secs(300);

fn apply_platform_proxy_policy(builder: ClientBuilder) -> ClientBuilder {
    #[cfg(target_os = "macos")]
    {
        // Avoid system proxy discovery on macOS because it can panic in restricted environments.
        builder.no_proxy()
    }
    #[cfg(not(target_os = "macos"))]
    {
        builder
    }
}

#[allow(clippy::panic)]
fn build_client<F>(configure: F) -> Client
where
    F: Fn(ClientBuilder) -> ClientBuilder,
{
    let primary_builder = configure(apply_platform_proxy_policy(ClientBuilder::new()));
    primary_builder.build().unwrap_or_else(|_| {
        let fallback_builder = apply_platform_proxy_policy(ClientBuilder::new())
            .timeout(DEFAULT_TIMEOUT)
            .connect_timeout(SHORT_TIMEOUT);
        fallback_builder
            .build()
            .unwrap_or_else(|e| panic!("failed to build HTTP client: {e}"))
    })
}

/// Create a default HTTP client with standard timeouts
pub fn create_default_client() -> Client {
    create_client_with_timeout(DEFAULT_TIMEOUT)
}

/// Create an HTTP client with a custom timeout
pub fn create_client_with_timeout(timeout: Duration) -> Client {
    build_client(|builder| builder.timeout(timeout).connect_timeout(SHORT_TIMEOUT))
}

/// Create an HTTP client with custom connect and request timeouts
pub fn create_client_with_timeouts(connect_timeout: Duration, request_timeout: Duration) -> Client {
    build_client(|builder| {
        builder
            .timeout(request_timeout)
            .connect_timeout(connect_timeout)
    })
}

/// Create an HTTP client with a specific user agent
pub fn create_client_with_user_agent(user_agent: &str) -> Client {
    build_client(|builder| builder.user_agent(user_agent).timeout(DEFAULT_TIMEOUT))
}

/// Create an HTTP client optimized for streaming
pub fn create_streaming_client() -> Client {
    build_client(|builder| {
        builder
            .connect_timeout(SHORT_TIMEOUT)
            .tcp_keepalive(Some(Duration::from_secs(60)))
    })
}

/// Get a default client or create one
pub fn get_or_create_default_client(existing: Option<Client>) -> Client {
    existing.unwrap_or_else(create_default_client)
}
