export type AdminRole = "SUPER_ADMIN" | "ADMIN" | "SALES" | "CONTENT_MANAGER";

export const ADMIN_ROLES: readonly AdminRole[] = ["SUPER_ADMIN", "ADMIN"];
export const SALES_ROLES: readonly AdminRole[] = [...ADMIN_ROLES, "SALES"];
export const CONTENT_ROLES: readonly AdminRole[] = [...ADMIN_ROLES, "CONTENT_MANAGER"];

export function isAdminRole(role: unknown): role is AdminRole {
  return (
    role === "SUPER_ADMIN" ||
    role === "ADMIN" ||
    role === "SALES" ||
    role === "CONTENT_MANAGER"
  );
}

export function hasAnyRole(role: unknown, allowed: readonly AdminRole[]): boolean {
  return isAdminRole(role) && allowed.includes(role);
}

export function allowedRolesForAdminPath(pathname: string): readonly AdminRole[] {
  if (pathname.startsWith("/admin/leads") || pathname.startsWith("/admin/test-rides")) {
    return SALES_ROLES;
  }

  if (
    pathname.startsWith("/admin/branches") ||
    pathname.startsWith("/admin/brands") ||
    pathname.startsWith("/admin/offers") ||
    pathname.startsWith("/admin/gallery") ||
    pathname.startsWith("/admin/cms") ||
    pathname.startsWith("/admin/parts") ||
    pathname.startsWith("/admin/products") ||
    pathname.startsWith("/admin/3d-assets")
  ) {
    return CONTENT_ROLES;
  }

  return ADMIN_ROLES;
}
