import { prisma } from "@/lib/prisma";

export function parseIngredientIds(raw: unknown): string[] | null {
  if (raw === undefined) return null;
  if (!Array.isArray(raw)) return [];
  return raw
    .filter((id): id is string => typeof id === "string")
    .map((id) => id.trim())
    .filter(Boolean);
}

export async function validateIngredientIds(ids: string[]): Promise<boolean> {
  if (ids.length === 0) return true;
  const count = await prisma.ingredient.count({
    where: { id: { in: ids } },
  });
  return count === ids.length;
}

export const waffleIngredientInclude = {
  ingredientItems: {
    orderBy: { name: "asc" as const },
    select: {
      id: true,
      name: true,
    },
  },
} as const;
