"""Feature definitions and default role permissions."""

from __future__ import annotations

from typing import Dict, Final, List, Set

from backend.constants.roles import UserRole

ROLE_FEATURES: Final[List[dict[str, str]]] = [
    {"key": "scan", "label": "Scan"},
    {"key": "process", "label": "Process"},
    {"key": "search_photos", "label": "Search Photos"},
    {"key": "identify_people", "label": "Identify People"},
    {"key": "auto_match", "label": "Auto-Match"},
    {"key": "modify_people", "label": "Modify People"},
    {"key": "tag_photos", "label": "Tag Photos"},
    {"key": "faces_maintenance", "label": "Faces Maintenance"},
    {"key": "user_identified", "label": "User Identified"},
    {"key": "user_reported", "label": "User Reported"},
    {"key": "user_tagged", "label": "User Tagged Photos"},
    {"key": "user_uploaded", "label": "User Uploaded"},
    {"key": "manage_users", "label": "Manage Users"},
    {"key": "manage_roles", "label": "Manage Roles"},
]

ROLE_FEATURE_KEYS: Final[List[str]] = [feature["key"] for feature in ROLE_FEATURES]

DEFAULT_ROLE_FEATURE_MATRIX: Final[Dict[str, Set[str]]] = {
    UserRole.ADMIN.value: set(ROLE_FEATURE_KEYS),
    UserRole.MANAGER.value: set(ROLE_FEATURE_KEYS),
    UserRole.MODERATOR.value: {"scan", "process", "manage_users"},
    UserRole.REVIEWER.value: {"user_identified", "user_reported", "user_uploaded", "user_tagged"},
    UserRole.EDITOR.value: {"user_identified", "user_uploaded", "manage_users", "user_tagged"},
    UserRole.IMPORTER.value: {"user_uploaded"},
    UserRole.VIEWER.value: {"user_identified", "user_reported", "user_tagged"},
}


def get_default_permission(role: str, feature_key: str) -> bool:
    """Return the default allowed value for a role/feature pair."""
    allowed_features = DEFAULT_ROLE_FEATURE_MATRIX.get(role, set())
    return feature_key in allowed_features

