/**
 * KRA - Access Control
 * 权限控制配置
 * @see https://umijs.org/docs/max/access#access
 */

export interface UserAuthority {
  authorityId: string | number;
  authorityName: string;
  defaultRouter?: string;
}

export interface CurrentUser {
  uuid?: string;
  userName?: string;
  nickName?: string;
  headerImg?: string;
  authority?: UserAuthority;
  authorities?: UserAuthority[];
  access?: string;
  // 按钮权限列表
  buttons?: string[];
  // 菜单权限列表
  menus?: string[];
}

export interface InitialState {
  currentUser?: CurrentUser;
  settings?: any;
  loading?: boolean;
}

export default function access(initialState: InitialState | undefined) {
  const { currentUser } = initialState ?? {};
  const authority = currentUser?.authority;
  const authorities = currentUser?.authorities || [];
  const buttons = currentUser?.buttons || [];
  const menus = currentUser?.menus || [];

  // 获取所有角色ID
  const authorityIds = authorities.map((a) => 
    typeof a.authorityId === 'string' ? parseInt(a.authorityId, 10) : a.authorityId
  );

  // 当前角色ID
  const currentAuthorityId = authority?.authorityId 
    ? (typeof authority.authorityId === 'string' 
        ? parseInt(authority.authorityId, 10) 
        : authority.authorityId)
    : 0;

  // 是否为超级管理员 (authorityId = 888)
  const isSuperAdmin = currentAuthorityId === 888 || authorityIds.includes(888);

  // 是否为管理员 (authorityId <= 1000 通常为管理员角色)
  const isAdmin = isSuperAdmin || currentAuthorityId <= 1000;

  return {
    // 是否已登录
    isLoggedIn: !!currentUser,

    // 是否为超级管理员
    isSuperAdmin,

    // 是否为管理员
    isAdmin,

    // 兼容旧版 canAdmin
    canAdmin: isAdmin,

    // 当前角色ID
    currentAuthorityId,

    // 所有角色ID列表
    authorityIds,

    // 按钮权限列表
    buttons,

    // 菜单权限列表
    menus,

    // 检查是否有指定角色
    hasAuthority: (authorityId: number) => {
      if (isSuperAdmin) return true;
      return authorityIds.includes(authorityId) || currentAuthorityId === authorityId;
    },

    // 检查是否有指定按钮权限
    hasButton: (buttonKey: string) => {
      if (isSuperAdmin) return true;
      return buttons.includes(buttonKey);
    },

    // 检查是否有指定菜单权限
    hasMenu: (menuPath: string) => {
      if (isSuperAdmin) return true;
      return menus.includes(menuPath);
    },

    // 检查路由访问权限
    canAccessRoute: (path: string) => {
      if (isSuperAdmin) return true;
      // 公开路由
      const publicRoutes = ['/user/login', '/user/register', '/init', '/404', '/403', '/500'];
      if (publicRoutes.some((route) => path.startsWith(route))) {
        return true;
      }
      // 检查菜单权限
      if (menus.length > 0) {
        return menus.some((menu) => path.startsWith(menu));
      }
      // 默认允许访问（如果没有配置菜单权限）
      return true;
    },

    // 检查是否有任一指定权限
    hasAnyAuthority: (ids: number[]) => {
      if (isSuperAdmin) return true;
      return ids.some((id) => authorityIds.includes(id) || currentAuthorityId === id);
    },

    // 检查是否有所有指定权限
    hasAllAuthorities: (ids: number[]) => {
      if (isSuperAdmin) return true;
      return ids.every((id) => authorityIds.includes(id) || currentAuthorityId === id);
    },
  };
}
