from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path

from taninux.core import theme as T


class Category(str, Enum):
    DOTFOLDERS = "dotfolders"
    PACMAN = "pacman"
    FLATPAK = "flatpak"
    AUR = "aur"
    SYSTEM = "system"


class Classification(str, Enum):
    ACTIVE = "active"
    WHITELISTED = "whitelisted"
    UNKNOWN = "unknown"
    LIKELY_ORPHAN = "likely"
    SAFE = "safe"


CLASSIFICATION_ORDER = [
    Classification.SAFE,
    Classification.LIKELY_ORPHAN,
    Classification.UNKNOWN,
    Classification.WHITELISTED,
    Classification.ACTIVE,
]

CLASS_COLOR = {
    Classification.SAFE: T.TAKE,
    Classification.LIKELY_ORPHAN: T.CHIKYU,
    Classification.UNKNOWN: T.BASE4,
    Classification.WHITELISTED: T.SHINKAI,
    Classification.ACTIVE: T.UME,
}

CLASS_GLYPH = {
    Classification.SAFE: "●",
    Classification.LIKELY_ORPHAN: "◐",
    Classification.UNKNOWN: "○",
    Classification.WHITELISTED: "◆",
    Classification.ACTIVE: "▲",
}

CLASS_LABEL = {
    Classification.SAFE: "sicher",
    Classification.LIKELY_ORPHAN: "vermutlich",
    Classification.UNKNOWN: "unklar",
    Classification.WHITELISTED: "behalten",
    Classification.ACTIVE: "aktiv",
}


def glyph(cls: Classification) -> str:
    return f"[{CLASS_COLOR[cls]}]{CLASS_GLYPH[cls]}[/]"


@dataclass
class Finding:
    path: Path
    category: Category
    classification: Classification
    size_bytes: int
    last_access_days: int | None
    reason: str
    matched_package: str | None = None
    extra: dict[str, str] = field(default_factory=dict)

    @property
    def sort_key(self) -> tuple[int, int]:
        return (CLASSIFICATION_ORDER.index(self.classification), -self.size_bytes)
