import { boolean, index, integer, numeric, pgTable, text, timestamp, uniqueIndex, } from "drizzle-orm/pg-core"; import { user } from "./auth"; import { foods, foodPortions } from "./foods"; import { portionUnitEnum } from "./enums"; export const recipes = pgTable( "recipes", { id: text("id").primaryKey(), ownerUserId: text("ownerUserId").notNull().references(() => user.id, { onDelete: "cascade" }), name: text("name").notNull(), portions: integer("portions").notNull().default(1), minutes: integer("minutes").notNull().default(0), notes: text("notes"), mealPrepDays: integer("mealPrepDays").notNull().default(0), isPublic: boolean("isPublic").notNull().default(false), createdAt: timestamp("createdAt").notNull().defaultNow(), updatedAt: timestamp("updatedAt").notNull().defaultNow(), }, (table) => ({ ownerIdx: index("recipes_owner_idx").on(table.ownerUserId), }), ); export const recipeIngredients = pgTable( "recipe_ingredients", { id: text("id").primaryKey(), recipeId: text("recipeId").notNull().references(() => recipes.id, { onDelete: "cascade" }), foodId: text("foodId").notNull().references(() => foods.id, { onDelete: "restrict" }), amountG: numeric("amountG", { precision: 8, scale: 2 }).notNull(), displayAmount: numeric("displayAmount", { precision: 8, scale: 2 }), displayUnit: portionUnitEnum("displayUnit"), portionId: text("portionId").references(() => foodPortions.id, { onDelete: "set null" }), sortOrder: integer("sortOrder").notNull().default(0), }, (table) => ({ recipeIdx: index("recipe_ingredients_recipe_idx").on(table.recipeId), }), ); export const recipeSubmissions = pgTable( "recipe_submissions", { id: text("id").primaryKey(), userId: text("userId").notNull().references(() => user.id, { onDelete: "cascade" }), name: text("name").notNull(), portions: integer("portions").notNull().default(1), minutes: integer("minutes").notNull().default(0), notes: text("notes"), mealPrepDays: integer("mealPrepDays").notNull().default(0), status: text("status").notNull().default("pending"), reviewNote: text("reviewNote"), reviewedBy: text("reviewedBy").references(() => user.id, { onDelete: "set null" }), reviewedAt: timestamp("reviewedAt"), acceptedRecipeId: text("acceptedRecipeId").references(() => recipes.id, { onDelete: "set null" }), createdAt: timestamp("createdAt").notNull().defaultNow(), updatedAt: timestamp("updatedAt").notNull().defaultNow(), }, (table) => ({ userIdx: index("recipe_submissions_user_idx").on(table.userId), statusIdx: index("recipe_submissions_status_idx").on(table.status, table.createdAt), }), ); export const recipeSubmissionIngredients = pgTable( "recipe_submission_ingredients", { id: text("id").primaryKey(), submissionId: text("submissionId").notNull().references(() => recipeSubmissions.id, { onDelete: "cascade" }), foodId: text("foodId").notNull().references(() => foods.id, { onDelete: "restrict" }), amountG: numeric("amountG", { precision: 8, scale: 2 }).notNull(), displayAmount: numeric("displayAmount", { precision: 8, scale: 2 }), displayUnit: portionUnitEnum("displayUnit"), portionId: text("portionId").references(() => foodPortions.id, { onDelete: "set null" }), sortOrder: integer("sortOrder").notNull().default(0), }, (table) => ({ submissionIdx: index("recipe_submission_ingredients_submission_idx").on(table.submissionId), }), ); export const recipeRatings = pgTable( "recipe_ratings", { id: text("id").primaryKey(), recipeId: text("recipeId").notNull().references(() => recipes.id, { onDelete: "cascade" }), userId: text("userId").notNull().references(() => user.id, { onDelete: "cascade" }), rating: integer("rating").notNull(), comment: text("comment"), createdAt: timestamp("createdAt").notNull().defaultNow(), updatedAt: timestamp("updatedAt").notNull().defaultNow(), }, (table) => ({ uniqueUserRecipe: uniqueIndex("recipe_ratings_user_recipe_unique").on(table.recipeId, table.userId), }), ); export type Recipe = typeof recipes.$inferSelect; export type NewRecipe = typeof recipes.$inferInsert;