"""Dangerous shell pattern value object."""

from __future__ import annotations

import re
from dataclasses import dataclass, field

from cleveragents.tui.shell_safety.danger_level import ShellDangerLevel


@dataclass(slots=True, frozen=True)
class DangerousPattern:
    """Immutable descriptor for a single dangerous shell pattern.

    Attributes:
        name: Short human-readable identifier (e.g. ``"rm_rf_root"``).
        pattern: Regular expression that matches the dangerous command text.
            The pattern is compiled with ``re.IGNORECASE`` by default.
        level: Danger severity classification.
        description: Human-readable explanation of why this pattern is
            dangerous and what damage it can cause.
        case_sensitive: When ``True`` the regex is compiled without
            ``re.IGNORECASE``.  Defaults to ``False``.
    """

    name: str
    pattern: str
    level: ShellDangerLevel
    description: str
    case_sensitive: bool = False

    # Compiled regex is stored in a non-frozen slot via __post_init__
    _compiled: re.Pattern[str] = field(init=False, compare=False, repr=False)

    def __post_init__(self) -> None:
        flags = 0 if self.case_sensitive else re.IGNORECASE
        # frozen=True prevents direct attribute assignment; use object.__setattr__
        object.__setattr__(self, "_compiled", re.compile(self.pattern, flags))

    def matches(self, command: str) -> bool:
        """Return ``True`` if *command* matches this pattern."""
        return bool(self._compiled.search(command))
