//! Message parsing for node discovery

use std::collections::HashMap;
use std::time::SystemTime;

use super::NodeInfo;

/// Parse node info from discovery message
pub fn parse_node_info(data: &serde_json::Value) -> Option<NodeInfo> {
    let node_id = data["node_id"].as_str()?.to_string();
    let node_type = data["node_type"].as_str().unwrap_or("unknown").to_string();
    let host = data["host"].as_str().unwrap_or("unknown").to_string();
    let port = data["port"].as_u64().unwrap_or(0) as u16;

    let mut metadata = HashMap::new();
    if let Some(meta_obj) = data["metadata"].as_object() {
        for (key, value) in meta_obj {
            if let Some(val_str) = value.as_str() {
                metadata.insert(key.clone(), val_str.to_string());
            }
        }
    }

    Some(NodeInfo {
        node_id,
        node_type,
        host,
        port,
        last_seen: SystemTime::now(),
        metadata,
    })
}
