CREATE TABLE "cooking_sessions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"recipe_id" uuid NOT NULL,
	"household_id" uuid NOT NULL,
	"started_by_user_id" uuid NOT NULL,
	"status" text DEFAULT 'planned' NOT NULL,
	"planned_portions" integer NOT NULL,
	"planned_meal_type" "meal_type" DEFAULT 'dinner' NOT NULL,
	"actual_portions_eaten" integer,
	"leftover_estimate_portions" integer,
	"planned_deductions" jsonb,
	"started_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"cancelled_at" timestamp with time zone,
	"cancel_reason" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "inventory_transactions" ADD COLUMN "cooking_session_id" uuid;--> statement-breakpoint
ALTER TABLE "recipe_cooks" ADD COLUMN "cooking_session_id" uuid;--> statement-breakpoint
ALTER TABLE "meal_boxes" ADD COLUMN "cooking_session_id" uuid;--> statement-breakpoint
ALTER TABLE "meals" ADD COLUMN "cooking_session_id" uuid;--> statement-breakpoint
ALTER TABLE "cooking_sessions" ADD CONSTRAINT "cooking_sessions_recipe_id_recipes_id_fk" FOREIGN KEY ("recipe_id") REFERENCES "public"."recipes"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cooking_sessions" ADD CONSTRAINT "cooking_sessions_household_id_households_id_fk" FOREIGN KEY ("household_id") REFERENCES "public"."households"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "cooking_sessions" ADD CONSTRAINT "cooking_sessions_started_by_user_id_users_id_fk" FOREIGN KEY ("started_by_user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "cooking_sessions_household_status_idx" ON "cooking_sessions" USING btree ("household_id","status");--> statement-breakpoint
CREATE INDEX "cooking_sessions_recipe_idx" ON "cooking_sessions" USING btree ("recipe_id");--> statement-breakpoint
CREATE INDEX "cooking_sessions_timeout_idx" ON "cooking_sessions" USING btree ("status","started_at");--> statement-breakpoint
ALTER TABLE "inventory_transactions" ADD CONSTRAINT "inventory_transactions_cooking_session_id_cooking_sessions_id_fk" FOREIGN KEY ("cooking_session_id") REFERENCES "public"."cooking_sessions"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "recipe_cooks" ADD CONSTRAINT "recipe_cooks_cooking_session_id_cooking_sessions_id_fk" FOREIGN KEY ("cooking_session_id") REFERENCES "public"."cooking_sessions"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "meal_boxes" ADD CONSTRAINT "meal_boxes_cooking_session_id_cooking_sessions_id_fk" FOREIGN KEY ("cooking_session_id") REFERENCES "public"."cooking_sessions"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "meals" ADD CONSTRAINT "meals_cooking_session_id_cooking_sessions_id_fk" FOREIGN KEY ("cooking_session_id") REFERENCES "public"."cooking_sessions"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "recipe_cooks_session_idx" ON "recipe_cooks" USING btree ("cooking_session_id");