#!/usr/bin/env python3
"""Abstract base class for all SystemMonitor modules."""

from abc import ABC, abstractmethod


class BaseModule(ABC):
    """Base class that all SystemMonitor modules must inherit from.

    Each module runs as a separate process managed by the Engine.
    """

    name: str = "unnamed"
    module_type: str = "passive"  # passive, active, stealth, intel, connectivity
    priority: int = 100           # OOM priority (lower = more important)
    dependencies: list = []       # Module names that must be running first
    requires_root: bool = False
    requires_capture_bus: bool = False

    def __init__(self, bus, state, config, engine=None):
        self.bus = bus
        self.state = state
        self.config = config
        self.engine = engine
        self._running = False
        self._pid = None
        self._start_time = None

    @abstractmethod
    def start(self) -> None:
        """Start the module. Called by the engine in the module's own process."""
        ...

    @abstractmethod
    def stop(self) -> None:
        """Stop the module gracefully."""
        ...

    @abstractmethod
    def status(self) -> dict:
        """Return module status dict.

        Returns:
            {"running": bool, "pid": int, "uptime": float, "ram_mb": float, "cpu_pct": float}
        """
        ...

    @abstractmethod
    def configure(self, config: dict) -> None:
        """Apply new configuration to a running module."""
        ...

    def health_check(self) -> bool:
        """Check if the module is healthy. Override for custom checks."""
        try:
            return self.status().get("running", False)
        except Exception:
            return False
