from typing import List
from .models import Classification, MountCandidate, RuleEvidence
from .defaults import *
from .utils import norm, path_contains, text_contains


def rule_minecraft(candidate: MountCandidate) -> List[RuleEvidence]:
    image = norm(candidate.image)
    target = norm(candidate.target)

    if any(h in image for h in MINECRAFT_IMAGE_HINTS):
        if any(p in target for p in MINECRAFT_CRITICAL_PATHS):
            return [RuleEvidence("minecraft_critical", Classification.CRITICAL, 45,
                [f"{candidate.target} looks like Minecraft world data"], ["minecraft"])]
        if any(p in target for p in MINECRAFT_IMPORTANT_PATHS):
            return [RuleEvidence("minecraft_important", Classification.IMPORTANT, 25,
                [f"{candidate.target} looks like Minecraft config/plugins"], ["minecraft"])]

    return []


def rule_database(candidate: MountCandidate) -> List[RuleEvidence]:
    if path_contains(candidate.target, DATABASE_PATH_KEYWORDS):
        return [RuleEvidence("db_path", Classification.CRITICAL, 40,
            [f"{candidate.target} is database path"], ["database"])]

    if text_contains(candidate.image, DATABASE_IMAGE_HINTS):
        return [RuleEvidence("db_image", Classification.CRITICAL, 25,
            [f"{candidate.image} looks like DB"], ["database"])]

    return []


def rule_config(candidate: MountCandidate) -> List[RuleEvidence]:
    if path_contains(candidate.target, CONFIG_PATH_KEYWORDS):
        return [RuleEvidence("config", Classification.IMPORTANT, 20,
            [f"{candidate.target} is config"], ["config"])]

    return []


def rule_data(candidate: MountCandidate) -> List[RuleEvidence]:
    if path_contains(candidate.target, DATA_PATH_KEYWORDS):
        return [RuleEvidence("data", Classification.IMPORTANT, 20,
            [f"{candidate.target} is data"], ["data"])]

    return []


def rule_ephemeral(candidate: MountCandidate) -> List[RuleEvidence]:
    if path_contains(candidate.target, EPHEMERAL_PATH_KEYWORDS):
        return [RuleEvidence("ephemeral", Classification.EPHEMERAL, 35,
            [f"{candidate.target} is temp/cache"], ["ephemeral"])]

    return []


def rule_logs(candidate: MountCandidate) -> List[RuleEvidence]:
    if path_contains(candidate.target, LOG_PATH_KEYWORDS):
        return [RuleEvidence("logs", Classification.OPTIONAL, 15,
            [f"{candidate.target} is logs"], ["logs"])]

    return []


DEFAULT_RULES = [
    rule_minecraft,
    rule_database,
    rule_config,
    rule_data,
    rule_ephemeral,
    rule_logs,
]
