//! The agent's SSH deploy public key — shown so a read-only deploy key can be
//! added to private git targets. (The legacy repositories CRUD moved to the
//! unified onboarding/targets API.)

use dioxus::prelude::*;

/// Fetch the agent's SSH deploy public key.
#[server]
pub async fn fetch_ssh_public_key() -> Result<String, ServerFnError> {
    let resp = super::agent_client::agent_get("/api/v1/settings/ssh-public-key")
        .await?
        .send()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;

    if !resp.status().is_success() {
        return Err(ServerFnError::new("SSH key not available".to_string()));
    }

    let body: serde_json::Value = resp
        .json()
        .await
        .map_err(|e| ServerFnError::new(e.to_string()))?;

    Ok(body
        .get("public_key")
        .and_then(|v| v.as_str())
        .unwrap_or("")
        .to_string())
}
