import { auth } from "@/auth";
import { redirect } from "next/navigation";
import { db } from "@/db";
import { users } from "@/db/schema";
import { eq } from "drizzle-orm";

export async function requireUser() {
  const session = await auth();
  if (!session?.user?.id) redirect("/login");
  return session.user;
}

export async function requireHousehold() {
  const user = await requireUser();
  if (!user.householdId) {
    const [u] = await db.select().from(users).where(eq(users.id, user.id));
    if (!u?.householdId) redirect("/onboarding");
    return { user, householdId: u.householdId };
  }
  return { user, householdId: user.householdId };
}
