import { redirect } from 'next/navigation';
import { getSessionUser } from './session';
import { resolveTenantId, type AuthUser } from './authz';

export async function getCurrentUser(): Promise<AuthUser | null> {
  return getSessionUser();
}

export async function requireUser(): Promise<AuthUser> {
  const user = await getSessionUser();
  if (!user) redirect('/login');
  return user;
}

export async function requireAdmin(): Promise<AuthUser> {
  const user = await getSessionUser();
  if (!user) redirect('/login');
  if (user.role !== 'admin') redirect('/panel');
  return user;
}

export async function getCurrentTenantId(): Promise<string> {
  const user = await getSessionUser();
  return resolveTenantId(user);
}
