import { db, eq } from "@haushaltsApp/db";
import { households } from "@haushaltsApp/db/schema";
import type { CreateHouseholdInput } from "@haushaltsApp/shared/types";

export async function createHousehold(ownerId: string, input: CreateHouseholdInput) {
  const [household] = await db.insert(households).values({
    ...input,
    ownerId,
  }).returning();
  return household;
}

export async function getHouseholdById(id: string) {
  const [household] = await db
    .select()
    .from(households)
    .where(eq(households.id, id));
  return household ?? null;
}

export async function getHouseholdsByOwner(ownerId: string) {
  return db.select().from(households).where(eq(households.ownerId, ownerId));
}
