"""
Nexus 功能验收目录 — 将自动化检查项映射到产品功能（供 run_nexus_acceptance 使用）。
"""

from __future__ import annotations

from dataclasses import dataclass


@dataclass(frozen=True)
class FeatureSpec:
    id: str
    name: str
    layer: str  # api | ui | unit | infra | integration | chain
    description: str
    keywords: tuple[str, ...]  # 匹配子进程输出中的 [PASS]/[FAIL] 行


# 顺序即报告展示顺序
FEATURES: tuple[FeatureSpec, ...] = (
    FeatureSpec("infra_health", "健康检查 /health", "infra", "Shell 守护与负载均衡探活", ("/health", "GET /health")),
    FeatureSpec("infra_app", "前端静态 /app/", "infra", "SPA 入口可访问", ("/app/", "static")),
    FeatureSpec("auth", "认证与 JWT", "api", "登录、me、刷新", ("/api/auth/", "Auth")),
    FeatureSpec("dashboard", "仪表盘统计", "api", "服务器统计与概览", ("/api/servers/stats", "stats")),
    FeatureSpec("servers", "服务器管理", "api", "CRUD、分类、同步日志", ("/api/servers/", "Server", "import")),
    FeatureSpec("files", "文件管理", "api", "browse、files-capability", ("browse", "files-capability", "sync/")),
    FeatureSpec("terminal", "Web 终端", "api", "快捷命令、webssh-token", ("terminal", "webssh", "quick-commands")),
    FeatureSpec("push_sync", "推送与同步", "api", "validate-source、sync", ("sync", "validate-source", "Push")),
    FeatureSpec("scripts", "脚本库", "api", "脚本 CRUD、执行记录", ("/api/scripts/", "Scripts")),
    FeatureSpec("schedules", "调度任务", "api", "调度 CRUD、target_path、next_run", ("/api/schedules/", "Schedules")),
    FeatureSpec("credentials", "凭据预设", "api", "密码/SSH 密钥预设", ("/api/presets/", "ssh-key", "Password")),
    FeatureSpec("retries", "重试队列", "api", "失败任务重试列表", ("/api/retries/", "Retry")),
    FeatureSpec("audit", "审计日志", "api", "操作审计分页", ("/api/audit/", "Audit")),
    FeatureSpec("alerts", "告警历史", "api", "告警列表与统计", ("alert-history", "Alerts")),
    FeatureSpec("assets", "资产管理", "api", "平台/节点/命令日志", ("/api/assets/", "Assets")),
    FeatureSpec("search", "全局搜索", "api", "跨资源搜索", ("/api/search/", "Search")),
    FeatureSpec("settings", "系统设置", "api", "动态配置、IP 白名单", ("/api/settings/", "Settings")),
    FeatureSpec("health_detail", "健康详情", "api", "/health/detail", ("/health/detail",)),
    FeatureSpec("ui_dashboard", "页面·仪表盘", "ui", "路由 /", ("01 login", "dashboard")),
    FeatureSpec("ui_servers", "页面·服务器", "ui", "路由 /servers", ("02 servers",)),
    FeatureSpec("ui_terminal", "页面·终端", "ui", "路由 /terminal", ("04 terminal",)),
    FeatureSpec("ui_files", "页面·文件", "ui", "路由 /files", ("05 files",)),
    FeatureSpec("ui_push", "页面·推送", "ui", "路由 /push", ("06 push",)),
    FeatureSpec("ui_scripts", "页面·脚本", "ui", "路由 /scripts", ("07 scripts",)),
    FeatureSpec("ui_executions", "页面·执行看板", "ui", "路由 /executions", ("E2E-EXEC", "executions",)),
    FeatureSpec("ui_credentials", "页面·凭据", "ui", "路由 /credentials", ("08 credentials",)),
    FeatureSpec("ui_schedules", "页面·调度", "ui", "路由 /schedules", ("09 schedules",)),
    FeatureSpec("ui_retries", "页面·重试", "ui", "路由 /retries", ("10 retries",)),
    FeatureSpec("ui_commands", "页面·命令日志", "ui", "路由 /commands 三视图", ("11 commands", "推送日志")),
    FeatureSpec("ui_alerts", "页面·告警", "ui", "路由 /alerts", ("12 alerts",)),
    FeatureSpec("ui_audit", "页面·审计", "ui", "路由 /audit", ("13 audit",)),
    FeatureSpec("ui_settings", "页面·设置", "ui", "路由 /settings", ("14 settings",)),
    FeatureSpec("ui_theme", "页面·主题与登出", "ui", "主题切换、退出", ("15 theme",)),
    FeatureSpec("unit_core", "单元测试（核心）", "unit", "路径校验、权限、提权逻辑", ("test_posix", "test_files", "test_unix", "test_remote", "test_schema")),
    FeatureSpec("integration_push", "集成·推送 accepted", "integration", "IT-PUSH-001 后台化", ("test_push_sync", "IT-PUSH", "accepted")),
    FeatureSpec("integration_api", "集成·API 域", "integration", "设置/凭据/审计/脚本", ("tests/integration", "integration")),
    FeatureSpec("chain_push", "链条·推送进度", "chain", "CH-PUSH-001 WS", ("test_push_async", "CH-PUSH", "chain")),
    FeatureSpec("chain_auth", "链条·认证", "chain", "CH-AUTH-001 login refresh", ("test_auth_flow", "CH-AUTH")),
    FeatureSpec("chain_batch", "链条·批量任务", "chain", "CH-BAT-001 category job", ("test_batch_job", "CH-BAT")),
    FeatureSpec("integration_files", "集成·文件浏览", "integration", "IT-FILE-001 browse JSON", ("test_files_browse", "IT-FILE")),
    FeatureSpec("integration_install", "集成·安装模式", "integration", "IT-INST-001 install gate", ("test_install_mode", "IT-INST")),
    FeatureSpec("ui_terminal_idle", "页面·终端 idle", "ui", "E2E-TERM-001", ("terminal.spec", "E2E-TERM")),
    FeatureSpec("ui_files", "页面·文件", "ui", "E2E-FILE-001", ("files.spec", "E2E-FILE")),
    FeatureSpec("ui_scripts", "页面·脚本", "ui", "E2E-SCR-001", ("scripts.spec",)),
    FeatureSpec("ui_credentials", "页面·凭据校验", "ui", "E2E-CRED-001", ("credentials.spec",)),
    FeatureSpec("ui_retries", "页面·重试", "ui", "E2E-RET-001", ("retries.spec",)),
    FeatureSpec("ui_settings", "页面·设置", "ui", "E2E-SET-001", ("settings.spec",)),
)


def match_feature(check_name: str) -> str:
    """Return feature id for a single check label."""
    low = check_name.lower()
    for spec in FEATURES:
        for kw in spec.keywords:
            if kw.lower() in low:
                return spec.id
    return "other"
