import { getCurrentUser } from "@/lib/auth";
import { verifyBrandAccess } from "@/lib/brand-access";
import { prisma } from "@/lib/prisma";
import { AuthError, ForbiddenError, NotFoundError } from "@/lib/api/errors";

export async function requireUser() {
  const user = await getCurrentUser();
  if (!user) throw new AuthError("Unauthorized");
  return user;
}

export async function requireUserAndBrand(brandId: string) {
  const user = await requireUser();
  const allowed = await verifyBrandAccess(user.id, brandId);
  if (!allowed) throw new ForbiddenError("Forbidden");
  const brand = await prisma.brand.findUnique({ where: { id: brandId } });
  if (!brand) throw new NotFoundError("Brand not found");
  return { user, brand };
}
