//! Low-level LM Studio API client implementation.
//!
//! Handles communication with LM Studio servers via HTTP endpoints for model
//! discovery, loading, and inference.

use std::io;
use std::path::Path;
use vtcode_config::VTCodeConfig;

/// LM Studio HTTP client for model management and inference.
#[derive(Clone)]
pub struct LMStudioClient {
    client: reqwest::Client,
    base_url: String,
}

const LMSTUDIO_CONNECTION_ERROR: &str = "LM Studio is not responding. Install from https://lmstudio.ai/download and run 'lms server start'.";

impl LMStudioClient {
    /// Create a client from the configured LM Studio provider.
    ///
    /// This verifies that the server is reachable before returning.
    pub async fn try_from_provider(_config: &VTCodeConfig) -> io::Result<Self> {
        // Get the LM Studio base URL from environment or use default
        let base_url = std::env::var("LMSTUDIO_URL")
            .ok()
            .or_else(|| Some("http://localhost:1234".to_string()))
            .ok_or_else(|| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    "LM Studio base_url not configured. Set LMSTUDIO_URL environment variable.",
                )
            })?;

        let client = vtcode_commons::http::create_client_with_timeouts(
            std::time::Duration::from_secs(5),
            std::time::Duration::from_secs(30),
        );

        let instance = LMStudioClient {
            client,
            base_url: base_url.to_string(),
        };

        instance.check_server().await?;
        Ok(instance)
    }

    /// Check if the LM Studio server is accessible.
    async fn check_server(&self) -> io::Result<()> {
        let url = format!("{}/models", self.base_url.trim_end_matches('/'));
        let response = self.client.get(&url).send().await;

        if let Ok(resp) = response {
            if resp.status().is_success() {
                Ok(())
            } else {
                Err(io::Error::other(format!(
                    "Server returned error: {} {}",
                    resp.status(),
                    LMSTUDIO_CONNECTION_ERROR
                )))
            }
        } else {
            Err(io::Error::other(LMSTUDIO_CONNECTION_ERROR))
        }
    }

    /// Load a model by sending a minimal request to warm up the server.
    ///
    /// This sends an empty input with max_output_tokens=1 to trigger model loading.
    pub async fn load_model(&self, model: &str) -> io::Result<()> {
        let url = format!("{}/responses", self.base_url.trim_end_matches('/'));
        let request_body = serde_json::json!({
            "model": model,
            "input": "",
            "max_output_tokens": 1
        });

        let response = self
            .client
            .post(&url)
            .header("Content-Type", "application/json")
            .json(&request_body)
            .send()
            .await
            .map_err(|e| io::Error::other(format!("Request failed: {e}")))?;

        if response.status().is_success() {
            tracing::info!("Successfully loaded model '{model}'");
            Ok(())
        } else {
            Err(io::Error::other(format!(
                "Failed to load model: {}",
                response.status()
            )))
        }
    }

    /// Fetch the list of models available on the LM Studio server.
    pub async fn fetch_models(&self) -> io::Result<Vec<String>> {
        let url = format!("{}/models", self.base_url.trim_end_matches('/'));
        let response = self
            .client
            .get(&url)
            .send()
            .await
            .map_err(|e| io::Error::other(format!("Request failed: {e}")))?;

        if response.status().is_success() {
            let json: serde_json::Value = response.json().await.map_err(|e| {
                io::Error::new(io::ErrorKind::InvalidData, format!("JSON parse error: {e}"))
            })?;

            let models = json["data"]
                .as_array()
                .ok_or_else(|| {
                    io::Error::new(io::ErrorKind::InvalidData, "No 'data' array in response")
                })?
                .iter()
                .filter_map(|model| model["id"].as_str())
                .map(ToString::to_string)
                .collect();

            Ok(models)
        } else {
            Err(io::Error::other(format!(
                "Failed to fetch models: {}",
                response.status()
            )))
        }
    }

    /// Download a model using the `lms` CLI tool.
    pub async fn download_model(&self, model: &str) -> io::Result<()> {
        let lms = Self::find_lms()?;
        tracing::info!(model, "downloading model");

        let status = std::process::Command::new(&lms)
            .args(["get", "--yes", model])
            .stdout(std::process::Stdio::inherit())
            .stderr(std::process::Stdio::null())
            .status()
            .map_err(|e| {
                io::Error::other(format!("Failed to execute '{lms} get --yes {model}': {e}"))
            })?;

        if !status.success() {
            return Err(io::Error::other(format!(
                "Model download failed with exit code: {}",
                status.code().unwrap_or(-1)
            )));
        }

        tracing::info!("Successfully downloaded model '{model}'");
        Ok(())
    }

    /// Find the `lms` CLI tool in PATH or fallback locations.
    fn find_lms() -> io::Result<String> {
        Self::find_lms_with_home_dir(None)
    }

    /// Find the `lms` CLI tool with optional home directory override.
    fn find_lms_with_home_dir(home_dir: Option<&str>) -> io::Result<String> {
        // First try 'lms' in PATH
        if which::which("lms").is_ok() {
            return Ok("lms".to_string());
        }

        // Platform-specific fallback paths
        let home = match home_dir {
            Some(dir) => dir.to_string(),
            None => {
                #[cfg(unix)]
                {
                    std::env::var("HOME").unwrap_or_default()
                }
                #[cfg(windows)]
                {
                    std::env::var("USERPROFILE").unwrap_or_default()
                }
            }
        };

        #[cfg(unix)]
        let fallback_path = format!("{home}/.lmstudio/bin/lms");
        #[cfg(windows)]
        let fallback_path = format!("{home}\\.lmstudio\\bin\\lms.exe");

        if Path::new(&fallback_path).exists() {
            Ok(fallback_path)
        } else {
            Err(io::Error::new(
                io::ErrorKind::NotFound,
                "LM Studio not found. Please install LM Studio from https://lmstudio.ai/",
            ))
        }
    }

    /// Low-level constructor given a raw host root (testing only).
    #[cfg(test)]
    fn from_host_root(host_root: impl Into<String>) -> Self {
        let client = vtcode_commons::http::create_client_with_timeouts(
            std::time::Duration::from_secs(5),
            std::time::Duration::from_secs(30),
        );

        Self {
            client,
            base_url: host_root.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used, clippy::unwrap_used)]

    use super::*;

    fn panic_message(payload: Box<dyn std::any::Any + Send>) -> String {
        if let Some(message) = payload.downcast_ref::<String>() {
            return message.clone();
        }
        if let Some(message) = payload.downcast_ref::<&str>() {
            return (*message).to_string();
        }
        "unknown panic".to_string()
    }

    async fn start_mock_server_or_skip() -> Option<wiremock::MockServer> {
        match tokio::spawn(async { wiremock::MockServer::start().await }).await {
            Ok(server) => Some(server),
            Err(err) if err.is_panic() => {
                let message = panic_message(err.into_panic());
                if message.contains("Operation not permitted")
                    || message.contains("PermissionDenied")
                {
                    return None;
                }
                panic!("mock server should start: {message}");
            }
            Err(err) => panic!("mock server task should complete: {err}"),
        }
    }

    #[tokio::test]
    async fn test_fetch_models_happy_path() {
        if std::env::var("VTCODE_SANDBOX_NETWORK_DISABLED").is_ok() {
            tracing::info!("VTCODE_SANDBOX_NETWORK_DISABLED is set; skipping test");
            return;
        }

        let Some(server) = start_mock_server_or_skip().await else {
            return;
        };
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/models"))
            .respond_with(
                wiremock::ResponseTemplate::new(200).set_body_raw(
                    serde_json::json!({
                        "data": [
                            {"id": "openai/gpt-oss-20b"},
                        ]
                    })
                    .to_string(),
                    "application/json",
                ),
            )
            .mount(&server)
            .await;

        let client = LMStudioClient::from_host_root(server.uri());
        let models = client.fetch_models().await.expect("fetch models");
        assert!(models.contains(&"openai/gpt-oss-20b".to_string()));
    }

    #[tokio::test]
    async fn test_fetch_models_no_data_array() {
        if std::env::var("VTCODE_SANDBOX_NETWORK_DISABLED").is_ok() {
            tracing::info!("VTCODE_SANDBOX_NETWORK_DISABLED is set; skipping test");
            return;
        }

        let Some(server) = start_mock_server_or_skip().await else {
            return;
        };
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/models"))
            .respond_with(
                wiremock::ResponseTemplate::new(200)
                    .set_body_raw(serde_json::json!({}).to_string(), "application/json"),
            )
            .mount(&server)
            .await;

        let client = LMStudioClient::from_host_root(server.uri());
        let result = client.fetch_models().await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("No 'data' array in response")
        );
    }

    #[tokio::test]
    async fn test_fetch_models_server_error() {
        if std::env::var("VTCODE_SANDBOX_NETWORK_DISABLED").is_ok() {
            tracing::info!("VTCODE_SANDBOX_NETWORK_DISABLED is set; skipping test");
            return;
        }

        let Some(server) = start_mock_server_or_skip().await else {
            return;
        };
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/models"))
            .respond_with(wiremock::ResponseTemplate::new(500))
            .mount(&server)
            .await;

        let client = LMStudioClient::from_host_root(server.uri());
        let result = client.fetch_models().await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Failed to fetch models: 500")
        );
    }

    #[tokio::test]
    async fn test_check_server_happy_path() {
        if std::env::var("VTCODE_SANDBOX_NETWORK_DISABLED").is_ok() {
            tracing::info!("VTCODE_SANDBOX_NETWORK_DISABLED is set; skipping test");
            return;
        }

        let Some(server) = start_mock_server_or_skip().await else {
            return;
        };
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/models"))
            .respond_with(wiremock::ResponseTemplate::new(200))
            .mount(&server)
            .await;

        let client = LMStudioClient::from_host_root(server.uri());
        client
            .check_server()
            .await
            .expect("server check should pass");
    }

    #[tokio::test]
    async fn test_check_server_error() {
        if std::env::var("VTCODE_SANDBOX_NETWORK_DISABLED").is_ok() {
            tracing::info!("VTCODE_SANDBOX_NETWORK_DISABLED is set; skipping test");
            return;
        }

        let Some(server) = start_mock_server_or_skip().await else {
            return;
        };
        wiremock::Mock::given(wiremock::matchers::method("GET"))
            .and(wiremock::matchers::path("/models"))
            .respond_with(wiremock::ResponseTemplate::new(404))
            .mount(&server)
            .await;

        let client = LMStudioClient::from_host_root(server.uri());
        let result = client.check_server().await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Server returned error: 404")
        );
    }

    #[test]
    fn test_find_lms() {
        let result = LMStudioClient::find_lms();
        match result {
            Ok(_) => {
                // lms was found in PATH - that's fine
            }
            Err(e) => {
                // Expected error when LM Studio not installed
                assert!(e.to_string().contains("LM Studio not found"));
            }
        }
    }

    #[test]
    fn test_find_lms_with_mock_home() {
        // Test fallback path construction without touching env vars
        #[cfg(unix)]
        {
            let result = LMStudioClient::find_lms_with_home_dir(Some("/test/home"));
            if let Err(e) = result {
                assert!(e.to_string().contains("LM Studio not found"));
            }
        }
        #[cfg(windows)]
        {
            let result = LMStudioClient::find_lms_with_home_dir(Some("C:\\test\\home"));
            if let Err(e) = result {
                assert!(e.to_string().contains("LM Studio not found"));
            }
        }
    }

    #[test]
    fn test_from_host_root() {
        let client = LMStudioClient::from_host_root("http://localhost:1234");
        assert_eq!(client.base_url, "http://localhost:1234");

        let client = LMStudioClient::from_host_root("https://example.com:8080/api");
        assert_eq!(client.base_url, "https://example.com:8080/api");
    }
}
