from typing import Dict


KEYWORD_SCORES: Dict[str, int] = {
    "auth": 5,
    "login": 5,
    "logout": 5,
    "oauth": 5,
    "sso": 5,
    "token": 5,
    "jwt": 5,
    "session": 5,
    "cookie": 5,
    "permission": 5,
    "rbac": 5,
    "acl": 5,
    "role": 5,
    "guard": 5,
    "middleware": 5,
    "security": 5,
    "crypto": 5,
    "encrypt": 5,
    "hash": 5,
    "secret": 5,
    "key": 5,
    "vault": 5,
    "cors": 4,
    "csrf": 4,
    "headers": 4,
    "api": 3,
    "controller": 3,
    "view": 3,
    "views": 3,
    "route": 3,
    "router": 3,
    "endpoint": 3,
    "request": 3,
    "http": 3,
    "client": 3,
    "requests": 3,
    "fetch": 3,
    "axios": 3,
    "webhook": 3,
    "redirect": 3,
    "upload": 2,
    "file": 2,
    "path": 2,
}

PREFERRED_EXTS = {".py", ".js", ".ts", ".vue", ".java", ".go", ".cs"}


def score_path(path: str) -> int:
    lowered = path.lower()
    score = 0
    for keyword, points in KEYWORD_SCORES.items():
        if keyword in lowered:
            score += points
    for ext in PREFERRED_EXTS:
        if lowered.endswith(ext):
            score += 1
            break
    return score


def sort_by_score(paths: list[str]) -> list[tuple[str, int]]:
    scored = [(path, score_path(path)) for path in paths]
    scored.sort(key=lambda item: (-item[1], item[0]))
    return scored
