import "server-only"; import type { PoolClient } from "pg"; // Ein Startergericht in den Pool des Haushalts übernehmen (Onboarding, Schritt 4: // „fünf aus dem Starter-Pool übernehmen“). Der Haushalt bekommt eine eigene // Kopie — Titel, Zutaten, Schritte —, die er frei ändern kann; die Vorlage // bleibt unberührt. Zweimal dasselbe übernehmen gibt das vorhandene Gericht // zurück (dishes_starter_once_idx). // // Aufruf in withHousehold(householdId, …). export async function adoptStarterDish( client: PoolClient, householdId: string, memberId: string, starterKey: string, ): Promise<{ dishId: string; created: boolean }> { const existing = await client.query<{ id: string }>("select id from dishes where starter_key = $1", [starterKey]); if (existing.rows[0]) return { dishId: existing.rows[0].id, created: false }; const { rows } = await client.query<{ id: string }>( `insert into dishes (household_id, title, source_locale, base, tags, effort_minutes, created_by, starter_key) select $1, s.title, s.locale, s.base, s.tags, s.effort_minutes, $2, s.key from starter_dishes s where s.key = $3 returning id`, [householdId, memberId, starterKey], ); const dishId = rows[0]?.id; if (!dishId) throw new Error(`unknown starter dish: ${starterKey}`); await client.query( `insert into dish_ingredients (household_id, dish_id, ingredient_id, qty_per_portion, unit, note, is_staple, position) select $1, $2, i.ingredient_id, i.qty_per_portion, i.unit, i.note, i.is_staple, i.position from starter_dish_ingredients i where i.starter_key = $3`, [householdId, dishId, starterKey], ); await client.query( `insert into dish_steps (household_id, dish_id, position, section, text, timer_seconds) select $1, $2, s.position, s.section, s.text, s.timer_seconds from starter_dish_steps s where s.starter_key = $3`, [householdId, dishId, starterKey], ); return { dishId, created: true }; }