import { initDb } from "@/lib/db";
import type { UserRole } from "@/types";

/** Load fresh user row for session authorization checks. */
export async function loadSessionUser(userId: string) {
  const { User } = initDb();
  return User.findByPk(userId);
}

/** Verify user is active; returns null if deactivated or missing. */
export async function requireActiveUser(userId: string) {
  const user = await loadSessionUser(userId);
  if (!user?.isActive) return null;
  return user;
}

/** Verify user is an active super_admin (always from DB, not stale JWT). */
export async function requireActiveSuperAdmin(userId: string) {
  const user = await requireActiveUser(userId);
  if (!user || (user.role as UserRole) !== "super_admin") return null;
  return user;
}
