export enum Role {
  USER = "user",
  ADMIN = "super_admin",
}

export const ROUTE_PERMISSIONS = {
  user: {
    roles: [Role.USER],
    routes: [
      "/profile",
      "/edit-profile",
      "/change-password",
      "/credit-history",
      "/dashboard",
      "/dashboard/packages",
    ],
  },

  admin: {
    roles: [Role.ADMIN],
    routes: ["/admin", "/admin/users", "/admin/packages"],
  },
} as const;

export function getRequiredRoles(pathname: string): Role[] | null {
  for (const permission of Object.values(ROUTE_PERMISSIONS)) {
    const matched = permission.routes.some((route) =>
      pathname.startsWith(route),
    );

    if (matched) {
      return [...permission.roles];
    }
  }

  return null;
}

export function hasAccess(pathname: string, role: Role): boolean {
  const requiredRoles = getRequiredRoles(pathname);

  if (!requiredRoles) {
    return true;
  }

  return requiredRoles.includes(role);
}
