// utils/api/promoteUser.ts
import { getApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getFunctions, httpsCallable } from 'firebase/functions';

/**
 * Promote (or demote) a user by UID.
 * Caller must already have role ≥ 7 or the callable will return 403/permission‑denied.
 */
export async function promoteUser(uid: string, role = 7): Promise<void> {
  // ⛔  Only run on the client
  if (import.meta.server) {
    throw new Error('promoteUser must be called from the client');
  }

  // Make sure our own token is fresh so the callable sees the admin claim
  await getAuth().currentUser?.getIdToken(true);

  // Call the Cloud Function
  const fn = httpsCallable(getFunctions(getApp()), 'promoteUser');
  await fn({ uid, role }); // { ok: true } on success
}
