"""Kernel log line classifier — priority-ordered pattern matching (D-08, D-09)."""

from __future__ import annotations

import re
from typing import Literal

ClassificationType = Literal["oops", "panic", "warn", "kasan", "ubsan", "normal"]

# Priority-ordered patterns (D-08: first match wins, D-09: case-sensitive)
_PATTERNS: list[tuple[re.Pattern[str], ClassificationType]] = [
    (re.compile(r"Kernel panic"), "panic"),
    (re.compile(r"Oops:"), "oops"),
    (re.compile(r"KASAN:"), "kasan"),
    (re.compile(r"UBSAN:"), "ubsan"),
    (re.compile(r"WARNING:"), "warn"),
]


def classify_line(text: str) -> ClassificationType:
    """Classify a dmesg text line by severity. Returns first matching category."""
    for pattern, classification in _PATTERNS:
        if pattern.search(text):
            return classification
    return "normal"
