# [DEF:backend.src.services.reports.type_profiles:Module]
# @TIER: CRITICAL
# @SEMANTICS: reports, type_profiles, normalization, fallback
# @PURPOSE: Deterministic mapping of plugin/task identifiers to canonical report task types and fallback profile metadata.
# @LAYER: Domain
# @RELATION: DEPENDS_ON -> backend.src.models.report.TaskType
# @INVARIANT: Unknown input always resolves to TaskType.UNKNOWN with a single fallback profile.

# [SECTION: IMPORTS]
from typing import Any, Dict, Optional

from ...core.logger import belief_scope
from ...models.report import TaskType
# [/SECTION]

# [DEF:PLUGIN_TO_TASK_TYPE:Data]
# @PURPOSE: Maps plugin identifiers to normalized report task types.
PLUGIN_TO_TASK_TYPE: Dict[str, TaskType] = {
    "llm_dashboard_validation": TaskType.LLM_VERIFICATION,
    "superset-backup": TaskType.BACKUP,
    "superset-migration": TaskType.MIGRATION,
    "documentation": TaskType.DOCUMENTATION,
}
# [/DEF:PLUGIN_TO_TASK_TYPE:Data]

# [DEF:TASK_TYPE_PROFILES:Data]
# @PURPOSE: Profile metadata registry for each normalized task type.
TASK_TYPE_PROFILES: Dict[TaskType, Dict[str, Any]] = {
    TaskType.LLM_VERIFICATION: {
        "display_label": "LLM Verification",
        "visual_variant": "llm",
        "icon_token": "sparkles",
        "emphasis_rules": ["summary", "status", "next_actions"],
        "fallback": False,
    },
    TaskType.BACKUP: {
        "display_label": "Backup",
        "visual_variant": "backup",
        "icon_token": "archive",
        "emphasis_rules": ["summary", "status", "updated_at"],
        "fallback": False,
    },
    TaskType.MIGRATION: {
        "display_label": "Migration",
        "visual_variant": "migration",
        "icon_token": "shuffle",
        "emphasis_rules": ["summary", "status", "error_context"],
        "fallback": False,
    },
    TaskType.DOCUMENTATION: {
        "display_label": "Documentation",
        "visual_variant": "documentation",
        "icon_token": "file-text",
        "emphasis_rules": ["summary", "status", "details"],
        "fallback": False,
    },
    TaskType.UNKNOWN: {
        "display_label": "Other / Unknown",
        "visual_variant": "unknown",
        "icon_token": "help-circle",
        "emphasis_rules": ["summary", "status"],
        "fallback": True,
    },
}
# [/DEF:TASK_TYPE_PROFILES:Data]


# [DEF:resolve_task_type:Function]
# @PURPOSE: Resolve canonical task type from plugin/task identifier with guaranteed fallback.
# @PRE: plugin_id may be None or unknown.
# @POST: Always returns one of TaskType enum values.
# @PARAM: plugin_id (Optional[str]) - Source plugin/task identifier from task record.
# @RETURN: TaskType - Resolved canonical type or UNKNOWN fallback.
#
# @TEST_CONTRACT: ResolveTaskType ->
# {
#   required_fields: {plugin_id: str},
#   invariants: ["returns TaskType.UNKNOWN for missing/unmapped plugin_id"]
# }
# @TEST_FIXTURE: valid_plugin -> {"plugin_id": "superset-migration"}
# @TEST_EDGE: empty_plugin -> {"plugin_id": ""}
# @TEST_EDGE: none_plugin -> {"plugin_id": None}
# @TEST_EDGE: unknown_plugin -> {"plugin_id": "invalid-plugin"}
# @TEST_INVARIANT: fallback_to_unknown -> verifies: [empty_plugin, none_plugin, unknown_plugin]
def resolve_task_type(plugin_id: Optional[str]) -> TaskType:
    with belief_scope("resolve_task_type"):
        normalized = (plugin_id or "").strip()
        if not normalized:
            return TaskType.UNKNOWN
        return PLUGIN_TO_TASK_TYPE.get(normalized, TaskType.UNKNOWN)
# [/DEF:resolve_task_type:Function]


# [DEF:get_type_profile:Function]
# @PURPOSE: Return deterministic profile metadata for a task type.
# @PRE: task_type may be known or unknown.
# @POST: Returns a profile dict and never raises for unknown types.
# @PARAM: task_type (TaskType) - Canonical task type.
# @RETURN: Dict[str, Any] - Profile metadata used by normalization and UI contracts.
#
# @TEST_CONTRACT: GetTypeProfile ->
# {
#   required_fields: {task_type: TaskType},
#   invariants: ["returns a valid metadata dictionary even for UNKNOWN"]
# }
# @TEST_FIXTURE: valid_profile -> {"task_type": "migration"}
# @TEST_EDGE: missing_profile -> {"task_type": "some_new_type"}
# @TEST_INVARIANT: always_returns_dict -> verifies: [valid_profile, missing_profile]
def get_type_profile(task_type: TaskType) -> Dict[str, Any]:
    with belief_scope("get_type_profile"):
        return TASK_TYPE_PROFILES.get(task_type, TASK_TYPE_PROFILES[TaskType.UNKNOWN])
# [/DEF:get_type_profile:Function]

# [/DEF:backend.src.services.reports.type_profiles:Module]