import { redirect } from 'next/navigation';

import { getSessionUser } from '@/lib/auth/session';

export async function requireLogin(): Promise<NonNullable<Awaited<ReturnType<typeof getSessionUser>>>> {
  const user = await getSessionUser();
  if (!user) {
    redirect('/admin/login');
  }
  if (!user.is_approved) {
    redirect('/admin/pending');
  }
  return user;
}

export async function requireAdmin(): Promise<NonNullable<Awaited<ReturnType<typeof getSessionUser>>>> {
  const user = await requireLogin();
  if (!user.is_admin) {
    redirect('/admin/no-access');
  }
  return user;
}
