import { supabase } from "@/integrations/supabase/client";

/**
 * Check whether the given user has any admin-level role.
 * Uses the `user_roles` table which is the SSOT for permissions.
 * Returns false on any error (fail-safe: don't accidentally block C-side users).
 */
export async function isAdminUser(userId: string): Promise<boolean> {
  if (!userId) return false;
  try {
    const { data, error } = await supabase
      .from("user_roles")
      .select("role")
      .eq("user_id", userId)
      .in("role", ["admin", "super_admin"])
      .limit(1)
      .maybeSingle();
    if (error) return false;
    return !!data;
  } catch {
    return false;
  }
}
