use crate::domain::service::Service;
use nodes_registry_sdk::{Node, NodeSysCap, NodeSysInfo, NodesRegistryClient};
use std::sync::Arc;
use toolkit_canonical_errors::CanonicalError;
use toolkit_macros::domain_model;

/// Local client implementation for the nodes registry
#[domain_model]
pub struct NodesRegistryLocalClient {
    service: Arc<Service>,
}

impl NodesRegistryLocalClient {
    #[must_use]
    pub fn new(service: Arc<Service>) -> Self {
        Self { service }
    }
}

#[async_trait::async_trait]
impl NodesRegistryClient for NodesRegistryLocalClient {
    async fn get_node(&self, id: uuid::Uuid) -> Result<Node, CanonicalError> {
        self.service.get_node(id).map_err(CanonicalError::from)
    }

    async fn list_nodes(&self) -> Result<Vec<Node>, CanonicalError> {
        Ok(self.service.list_nodes())
    }

    async fn get_node_sysinfo(&self, node_id: uuid::Uuid) -> Result<NodeSysInfo, CanonicalError> {
        self.service
            .get_node_sysinfo(node_id)
            .map_err(CanonicalError::from)
    }

    async fn get_node_syscap(&self, node_id: uuid::Uuid) -> Result<NodeSysCap, CanonicalError> {
        self.service
            .get_node_syscap(node_id, false)
            .map_err(CanonicalError::from)
    }
}
