use crate::api::MyceliumClient;
use crate::error::AppResult;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NodeInfo {
    pub node_subnet: String,
    pub node_pubkey: String,
}

impl MyceliumClient {
    pub async fn node_info(&self) -> AppResult<NodeInfo> {
        let resp = self.http().get(self.url("/admin")).send().await?;
        Self::parse(resp).await
    }

    /// Cheap liveness probe used by the supervisor's health-check loop.
    /// Returns `true` if `/admin` answered 2xx.
    pub async fn is_alive(&self) -> bool {
        match self.http().get(self.url("/admin")).send().await {
            Ok(r) => r.status().is_success(),
            Err(_) => false,
        }
    }
}
