# Automatically generated by generate_types.py. Do not edit manually.
import enum

from pydantic import field_validator

from .base import CommonEntityType, load_entity_supported_types

__all__ = ["DetectionRuleType", "DetectionRuleTypes"]


class DetectionRuleType(CommonEntityType):
    """
    DetectionRuleType represents metadata for detection rules in Colander. Check :ref:`the list of supported
    types <detection_rule_types>`.

    Example:
        >>> detection_rule_type = DetectionRuleTypes.YARA.value
        >>> print(detection_rule_type.name)
        Yara rule
    """

    @field_validator("short_name", mode="before")
    @classmethod
    def is_supported_type(cls, short_name: str):
        if short_name not in {t["short_name"] for t in load_entity_supported_types("detection_rule")}:
            raise ValueError(f"{short_name} is not supported")
        return short_name


class DetectionRuleTypes(enum.Enum):
    """
    DetectionRuleTypes provides access to all supported detection rule types.

    This class loads detection rule type definitions from the detection rule types JSON file and exposes them as an enum.
    It also provides a method to look up a detection rule type by its short name.

    Example:
        >>> detection_rule_type = DetectionRuleTypes.YARA.value
        >>> print(detection_rule_type.name)
        Yara rule
        >>> default_type = DetectionRuleTypes.by_short_name("nonexistent")
        >>> print(default_type.name)
        Generic
    """

    GENERIC = DetectionRuleType(
        **{
            "short_name": "GENERIC",
            "name": "Generic",
            "description": "A general or unspecified detection rule type that does not fit other categories.",
            "icon": "",
            "nf_icon": "nf-cod-server_process",
        }
    )
    """Generic - A general or unspecified detection rule type that does not fit other categories."""

    SURICATA = DetectionRuleType(
        **{
            "short_name": "SURICATA",
            "name": "Suricata rule",
            "description": "A rule for the Suricata engine to detect network threats and suspicious traffic.",
            "icon": "",
            "nf_icon": "nf-cod-server_process",
        }
    )
    """Suricata rule - A rule for the Suricata engine to detect network threats and suspicious traffic."""

    YARA = DetectionRuleType(
        **{
            "short_name": "YARA",
            "name": "Yara rule",
            "description": "A rule used to identify and classify malware or files based on patterns.",
            "icon": "",
            "nf_icon": "nf-cod-server_process",
        }
    )
    """Yara rule - A rule used to identify and classify malware or files based on patterns."""

    default = GENERIC  # type: ignore[attr-defined]

    @classmethod
    def by_short_name(cls, short_name: str):
        sn = short_name.replace(" ", "_").upper()
        if sn in cls._member_names_:
            return cls[sn].value
        return cls.default.value

    @classmethod
    def suggest(cls, value: str) -> DetectionRuleType:
        return cls.by_short_name(value)
