//! Hypervisor backend registry

use dashmap::DashMap;
use plasmavmc_types::HypervisorType;
use std::sync::Arc;

use crate::HypervisorBackend;

/// Registry of available hypervisor backends
pub struct HypervisorRegistry {
    backends: DashMap<HypervisorType, Arc<dyn HypervisorBackend>>,
}

impl HypervisorRegistry {
    /// Create a new empty registry
    pub fn new() -> Self {
        Self {
            backends: DashMap::new(),
        }
    }

    /// Register a hypervisor backend
    pub fn register(&self, backend: Arc<dyn HypervisorBackend>) {
        let typ = backend.backend_type();
        self.backends.insert(typ, backend);
    }

    /// Get a backend by type
    pub fn get(&self, typ: HypervisorType) -> Option<Arc<dyn HypervisorBackend>> {
        self.backends.get(&typ).map(|r| r.value().clone())
    }

    /// List available backend types
    pub fn available(&self) -> Vec<HypervisorType> {
        self.backends.iter().map(|r| *r.key()).collect()
    }

    /// Check if a backend is registered
    pub fn has(&self, typ: HypervisorType) -> bool {
        self.backends.contains_key(&typ)
    }
}

impl Default for HypervisorRegistry {
    fn default() -> Self {
        Self::new()
    }
}
