import { getUsers, getUserByEmail } from 'officerdb';

// Identifies the platform owner ("Super Admin"). Two mechanisms, both supported:
//  (b) explicit  — SUPER_ADMIN_EMAIL in .env designates the owner by email.
//  (a) automatic — if that's unset, the bootstrap/first account (lowest user id) is the owner.
// Resolved once and cached: the owner never changes at runtime (bootstrap is closed after user #1).
const { SUPER_ADMIN_EMAIL } = process.env;

let cachedId: number | null = null;
let resolved = false;

async function resolveSuperAdminId(): Promise<number | null> {
  if (resolved) return cachedId;
  if (SUPER_ADMIN_EMAIL) {
    const user = await getUserByEmail(SUPER_ADMIN_EMAIL);
    cachedId = user?.id ?? null;
  } else {
    const users = await getUsers();
    cachedId = users.length ? users.reduce((min, u) => (u.id < min ? u.id : min), users[0]!.id) : null;
  }
  resolved = true;
  return cachedId;
}

export async function getSuperAdminId(): Promise<number | null> {
  try {
    return await resolveSuperAdminId();
  } catch {
    // Transient DB error before the id is cached: stay unresolved (deny) and retry next call.
    return null;
  }
}

export async function isSuperAdmin(payload: { id?: number } | null | undefined): Promise<boolean> {
  if (!payload?.id) return false;
  const adminId = await getSuperAdminId();
  return adminId !== null && payload.id === adminId;
}
