const DEFAULT_PAGE_PATHS_BY_ROLE = {
  admin: ["/", "/cases", "/deadlines", "/meetings", "/agenda-items", "/decisions", "/reports", "/finance", "/tasks", "/calendar", "/documents", "/comments", "/notifications", "/users", "/committees", "/departments", "/document-categories", "/public-documents-admin", "/templates", "/permissions", "/security", "/audit"],
  chair: ["/", "/cases", "/deadlines", "/meetings", "/agenda-items", "/decisions", "/reports", "/finance", "/tasks", "/calendar", "/documents", "/comments", "/notifications", "/committees", "/departments", "/document-categories", "/public-documents-admin", "/templates"],
  secretary: ["/", "/meetings", "/agenda-items", "/decisions", "/reports", "/finance", "/tasks", "/calendar", "/documents", "/comments", "/notifications"],
  member: ["/", "/cases", "/deadlines", "/tasks", "/calendar", "/documents", "/comments", "/notifications"],
  advisor: ["/", "/calendar", "/documents", "/comments", "/notifications"],
};

export function getAllowedPagePaths(user) {
  if (Array.isArray(user?.allowed_page_paths) && user.allowed_page_paths.length) {
    return user.allowed_page_paths;
  }
  return DEFAULT_PAGE_PATHS_BY_ROLE[user?.role] || ["/"];
}

export function canViewPath(user, path) {
  if (!user) return false;
  if (user.role === "admin") return true;
  const allowed = getAllowedPagePaths(user);
  return allowed.includes(path);
}

export function hasPermission(user, permissionKey) {
  if (!user) return false;
  if (user.role === "admin") return true;
  return Array.isArray(user.permissions) && user.permissions.includes(permissionKey);
}
