"""DangerousCommandWarning value object."""

from __future__ import annotations

from dataclasses import dataclass

from cleveragents.tui.shell_safety.danger_level import ShellDangerLevel
from cleveragents.tui.shell_safety.dangerous_pattern import DangerousPattern


@dataclass(slots=True, frozen=True)
class DangerousCommandWarning:
    """Immutable value object describing a detected dangerous command.

    Produced by
    :class:`~cleveragents.tui.shell_safety.pattern_detector.DangerousPatternDetector`
    and consumed by the TUI to present a confirmation dialog to the user.

    Attributes:
        command: The original command string that triggered the warning.
        matched_pattern: The :class:`DangerousPattern` that matched.
        danger_level: Convenience alias for ``matched_pattern.level``.
        message: Human-readable warning message suitable for display.
    """

    command: str
    matched_pattern: DangerousPattern
    danger_level: ShellDangerLevel
    message: str

    @classmethod
    def from_pattern(
        cls,
        command: str,
        pattern: DangerousPattern,
    ) -> DangerousCommandWarning:
        """Construct a warning from a command and the pattern that matched it.

        The ``message`` is derived from the pattern's ``description``.
        """
        level_name = pattern.level.name.capitalize()
        message = f"[{level_name}] Dangerous command detected: {pattern.description}"
        return cls(
            command=command,
            matched_pattern=pattern,
            danger_level=pattern.level,
            message=message,
        )
