namespace MiGu.Server.Auth;

/// <summary>
/// 单个「权限页面」定义。Key 与前端 vue-router 的 route.name 一一对齐，
/// 角色通过勾选 Key 集合决定可访问的页面（菜单 + 路由守卫据此放行）。
/// </summary>
public sealed record PageDef(string Key, string Label, string Group, string Scope);

/// <summary>
/// 平台「权限页面清单」——RBAC 的最小授权单元。
///
/// 设计：页面是由前端路由静态决定的（相对稳定），因此后端维护一份与
/// <c>frontends/.../router/index.ts</c> 对齐的静态清单，通过 <c>GET /api/rbac/pages</c>
/// 暴露给「权限与角色」管理页，让管理员可视化地把页面分配给角色。
///
/// Scope 含义：
/// - <c>Platform</c>：管理端（/admin/*）页面；
/// - <c>RCSMonitor</c>：运营监控端（/monitor/*）页面。
/// </summary>
public static class PageCatalog
{
    public const string ScopePlatform = "Platform";
    public const string ScopeMonitor = "RCSMonitor";

    /// <summary>权限页面通配符：角色 Pages 含此值表示「该 scope 下全部页面」（超级管理员）。</summary>
    public const string Wildcard = "*";

    public static readonly IReadOnlyList<PageDef> All = new List<PageDef>
    {
        // ── 管理端 / Platform：概览 ──
        new("admin-dashboard", "总览", "概览", ScopePlatform),
        new("admin-map-monitor", "地图监控", "概览", ScopePlatform),
        new("admin-playback", "调度回放", "概览", ScopePlatform),

        // ── 管理端 / Platform：设计与编排 ──
        new("admin-map-editor", "地图编辑", "设计与编排", ScopePlatform),
        new("admin-project-properties", "项目属性", "设计与编排", ScopePlatform),
        new("admin-tracks", "场景管理", "设计与编排", ScopePlatform),
        new("admin-cars", "车辆管理", "设计与编排", ScopePlatform),
        new("admin-processes", "进程管理", "设计与编排", ScopePlatform),
        new("admin-scripts", "脚本管理", "设计与编排", ScopePlatform),
        new("admin-missions", "任务编排", "设计与编排", ScopePlatform),

        // ── 管理端 / Platform：平台配置中心 ──
        new("admin-config-system", "系统级配置", "平台配置中心", ScopePlatform),
        new("admin-config-integrations", "外部系统对接", "平台配置中心", ScopePlatform),
        new("admin-config-routing", "路径规划", "平台配置中心", ScopePlatform),
        new("admin-config-vehicle", "车辆维护", "平台配置中心", ScopePlatform),
        new("admin-config-charge", "充电策略", "平台配置中心", ScopePlatform),
        new("admin-config-task", "任务分配", "平台配置中心", ScopePlatform),
        new("admin-config-traffic", "交通管制", "平台配置中心", ScopePlatform),
        new("admin-config-auth", "权限与角色", "平台配置中心", ScopePlatform),
        new("admin-config-device", "设备接入", "平台配置中心", ScopePlatform),
        new("admin-config-fleet", "车队生命周期", "平台配置中心", ScopePlatform),
        new("admin-config-scenario", "场景模板", "平台配置中心", ScopePlatform),
        new("admin-config-location", "库位管理", "平台配置中心", ScopePlatform),
        new("admin-config-ops", "运营维护", "平台配置中心", ScopePlatform),
        new("admin-config-widget", "自定义控件", "平台配置中心", ScopePlatform),
        new("admin-config-map-monitor", "地图监控配置", "平台配置中心", ScopePlatform),

        // ── 运营端 / RCSMonitor ──
        new("monitor-dashboard", "运营总览", "运营监控", ScopeMonitor),
        new("monitor-map", "地图监控", "运营监控", ScopeMonitor),
        new("monitor-ops", "运维操作", "运营监控", ScopeMonitor),
        new("monitor-notes", "运营备注", "运营监控", ScopeMonitor),
    };

    private static readonly HashSet<string> _keys =
        All.Select(p => p.Key).ToHashSet(StringComparer.OrdinalIgnoreCase);

    /// <summary>判断页面 Key 是否合法（用于角色保存时过滤掉脏数据 / 已下线页面）。</summary>
    public static bool IsValidKey(string key) => _keys.Contains(key);

    /// <summary>列出某 scope 下的全部页面 Key（用于把角色的 "*" 通配展开成具体页面集合）。</summary>
    public static IReadOnlyList<string> KeysForScope(string scope) =>
        All.Where(p => string.Equals(p.Scope, scope, StringComparison.OrdinalIgnoreCase))
           .Select(p => p.Key)
           .ToList();
}
