export type PermissionAccessLevel = "none" | "view" | "review" | "export" | "manage" | "special";

export type PermissionGroupProfile = {
  key: string;
  /** 仅平台超管在角色配置里可见（接入规则、全局 RBAC 等） */
  platformOnly?: boolean;
  levels: Array<{
    key: PermissionAccessLevel;
    labelKey: string;
    slugs: readonly string[];
  }>;
};

/** 客户配角色时用的「能做什么」档位，底层仍映射到 prd.* slug。 */
export const ADMIN_PERMISSION_GROUP_PROFILES: PermissionGroupProfile[] = [
  {
    key: "dashboard",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.dashboard.view"] },
    ],
  },
  {
    key: "agents",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      {
        key: "view",
        labelKey: "permissionAccess.view",
        slugs: ["prd.agent.view", "prd.agent.role.view", "prd.agent.user.view"],
      },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: [
          "prd.agent.view",
          "prd.agent.manage",
          "prd.agent.role.view",
          "prd.agent.role.manage",
          "prd.agent.user.view",
          "prd.agent.user.manage",
        ],
      },
    ],
  },
  {
    key: "players",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      {
        key: "view",
        labelKey: "permissionAccess.view",
        slugs: ["prd.users.view_finance", "prd.users.view_cs"],
      },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: ["prd.users.view_finance", "prd.users.view_cs", "prd.users.manage", "prd.player_freeze.manage"],
      },
    ],
  },
  {
    key: "draws",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.draw_result.view"] },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: ["prd.draw_result.view", "prd.draw_result.manage", "prd.draw_reopen.manage"],
      },
    ],
  },
  {
    key: "tickets",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.tickets.view"] },
    ],
  },
  {
    key: "settlement",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      {
        key: "view",
        labelKey: "permissionAccess.view",
        slugs: ["prd.payout.view", "prd.settlement.agent.view"],
      },
      {
        key: "review",
        labelKey: "permissionAccess.review",
        slugs: ["prd.payout.view", "prd.payout.review", "prd.settlement.agent.view"],
      },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: [
          "prd.payout.view",
          "prd.payout.review",
          "prd.payout.manage",
          "prd.settlement.agent.view",
          "prd.settlement.agent.manage",
        ],
      },
    ],
  },
  {
    key: "wallet",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      {
        key: "view",
        labelKey: "permissionAccess.view",
        slugs: ["prd.wallet_reconcile.view", "prd.wallet_reconcile.view_cs", "prd.users.view_finance"],
      },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: [
          "prd.wallet_reconcile.view",
          "prd.wallet_reconcile.view_cs",
          "prd.users.view_finance",
          "prd.wallet_reconcile.manage",
          "prd.wallet_adjust.manage",
        ],
      },
    ],
  },
  {
    key: "reconcile",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      {
        key: "view",
        labelKey: "permissionAccess.view",
        slugs: ["prd.wallet_reconcile.view", "prd.wallet_reconcile.view_cs"],
      },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.wallet_reconcile.view", "prd.wallet_reconcile.view_cs", "prd.wallet_reconcile.manage"] },
    ],
  },
  {
    key: "reports",
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.report.view"] },
      {
        key: "export",
        labelKey: "permissionAccess.export",
        slugs: ["prd.report.view", "prd.report.export"],
      },
    ],
  },
  {
    key: "admin_users",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.admin_user.manage"] },
    ],
  },
  {
    key: "admin_roles",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.admin_role.manage"] },
    ],
  },
  {
    key: "integration",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.integration.view"] },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.integration.view", "prd.integration.manage"] },
    ],
  },
  {
    key: "rules_plays",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.odds.view"] },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: ["prd.odds.view", "prd.play_switch.manage", "prd.odds.manage"],
      },
    ],
  },
  {
    key: "rules_odds",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.rebate.view"] },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: ["prd.rebate.view", "prd.rebate.manage", "prd.odds.manage"],
      },
    ],
  },
  {
    key: "risk_cap",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.risk_cap.view"] },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.risk_cap.view", "prd.risk_cap.manage"] },
    ],
  },
  {
    key: "risk",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.risk.view"] },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.risk.view", "prd.risk.manage"] },
    ],
  },
  {
    key: "jackpot",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.jackpot.view"] },
      {
        key: "manage",
        labelKey: "permissionAccess.manage",
        slugs: ["prd.jackpot.view", "prd.jackpot.manage"],
      },
      {
        key: "special",
        labelKey: "permissionAccess.special",
        slugs: ["prd.jackpot.view", "prd.jackpot.manage", "prd.jackpot.manual_burst"],
      },
    ],
  },
  {
    key: "currencies",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.currency.manage"] },
    ],
  },
  {
    key: "audit",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "view", labelKey: "permissionAccess.view", slugs: ["prd.audit.view"] },
    ],
  },
  {
    key: "settings",
    platformOnly: true,
    levels: [
      { key: "none", labelKey: "permissionAccess.none", slugs: [] },
      { key: "manage", labelKey: "permissionAccess.manage", slugs: ["prd.wallet_reconcile.manage", "prd.currency.manage"] },
    ],
  },
];

const PROFILE_BY_KEY = new Map(ADMIN_PERMISSION_GROUP_PROFILES.map((profile) => [profile.key, profile]));

export function permissionProfileForGroup(key: string): PermissionGroupProfile | undefined {
  return PROFILE_BY_KEY.get(key);
}

export function allSlugsForProfile(profile: PermissionGroupProfile): string[] {
  const out = new Set<string>();
  for (const level of profile.levels) {
    for (const slug of level.slugs) {
      out.add(slug);
    }
  }
  return Array.from(out);
}

export function resolveProfileLevel(profile: PermissionGroupProfile, selectedSlugs: readonly string[]): PermissionAccessLevel {
  const selected = new Set(selectedSlugs);
  let matched: PermissionAccessLevel = "none";
  const order: PermissionAccessLevel[] = ["view", "review", "export", "manage", "special"];

  for (const levelKey of order) {
    const level = profile.levels.find((item) => item.key === levelKey);
    if (level === undefined || level.slugs.length === 0) {
      continue;
    }
    if (level.slugs.every((slug) => selected.has(slug))) {
      matched = level.key;
    }
  }

  return matched;
}

export function applyProfileLevel(
  profile: PermissionGroupProfile,
  levelKey: PermissionAccessLevel,
  selectedSlugs: readonly string[],
): string[] {
  const groupSlugs = new Set(allSlugsForProfile(profile));
  const next = selectedSlugs.filter((slug) => !groupSlugs.has(slug));
  const level = profile.levels.find((item) => item.key === levelKey);
  if (level) {
    next.push(...level.slugs);
  }
  return Array.from(new Set(next)).sort();
}