"""Scanner-Registry: jeder Eintrag wird zu einem Tab in der TUI."""
from __future__ import annotations

from dataclasses import dataclass
from typing import Callable

from taninux.maintain.scanners.aur import AurCacheScanner
from taninux.maintain.scanners.base import Scanner
from taninux.maintain.scanners.dotfolders import DotfoldersScanner
from taninux.maintain.scanners.flatpak import FlatpakDataScanner, FlatpakRuntimesScanner
from taninux.maintain.scanners.pacman import (
    PacmanCacheScanner,
    PacmanOrphansScanner,
    PacnewScanner,
)
from taninux.maintain.scanners.system import (
    SystemCoredumpScanner,
    SystemJournalScanner,
    SystemKernelsScanner,
)


@dataclass(frozen=True)
class ScannerSpec:
    id: str
    label: str
    factory: Callable[[], Scanner]
    # Welche TUI-Aktionen sind im jeweiligen Tab sinnvoll?
    # - 'trash':  → move-to-trash der User-File (dotfolders)
    # - 'view':   → nur anzeigen + Hinweis-Command im Detail-Pane
    actions: str = "view"
    advice: str = ""


SCANNERS: list[ScannerSpec] = [
    ScannerSpec(
        id="dotfolders",
        label="dotfolders",
        factory=DotfoldersScanner,
        actions="trash",
        advice="",
    ),
    ScannerSpec(
        id="orphans",
        label="orphans",
        factory=PacmanOrphansScanner,
        actions="view",
        advice="entfernen mit:  sudo pacman -Rns {pkg}",
    ),
    ScannerSpec(
        id="pacnew",
        label="pacnew",
        factory=PacnewScanner,
        actions="view",
        advice="review/merge mit:  sudo pacdiff   (oder: sudo $EDITOR {path})",
    ),
    ScannerSpec(
        id="cache",
        label="cache",
        factory=PacmanCacheScanner,
        actions="view",
        advice="cleanup mit:  sudo paccache -ruk0   (entfernt uninstalled + alte Versionen)",
    ),
    ScannerSpec(
        id="flatpak-runtimes",
        label="fp-runtimes",
        factory=FlatpakRuntimesScanner,
        actions="view",
        advice="cleanup mit:  flatpak uninstall --unused   (löst Extension-Ketten korrekt auf)",
    ),
    ScannerSpec(
        id="flatpak-data",
        label="fp-data",
        factory=FlatpakDataScanner,
        actions="trash",
        advice="",
    ),
    ScannerSpec(
        id="aur-cache",
        label="aur-cache",
        factory=AurCacheScanner,
        actions="trash",
        advice="",
    ),
    ScannerSpec(
        id="journal",
        label="journal",
        factory=SystemJournalScanner,
        actions="view",
        advice="vacuum:  sudo journalctl --vacuum-time=2weeks   (oder --vacuum-size=200M)",
    ),
    ScannerSpec(
        id="coredumps",
        label="coredumps",
        factory=SystemCoredumpScanner,
        actions="view",
        advice="cleanup:  sudo coredumpctl --since=2weeksago delete   (oder: sudo rm /var/lib/systemd/coredump/*)",
    ),
    ScannerSpec(
        id="kernels",
        label="kernels",
        factory=SystemKernelsScanner,
        actions="view",
        advice="entfernen:  sudo rm -r {path}   (vorsicht: nur wenn das System nicht mehr von diesem Kernel bootet!)",
    ),
]


def by_id(scanner_id: str) -> ScannerSpec | None:
    for s in SCANNERS:
        if s.id == scanner_id:
            return s
    return None
