CREATE TABLE "page_layout_pointers" (
	"page" text PRIMARY KEY NOT NULL,
	"live_version_id" uuid NOT NULL,
	"draft_body" jsonb,
	"draft_updated_at" timestamp with time zone,
	"draft_updated_by" uuid,
	"scheduled_version_id" uuid,
	"scheduled_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "page_layouts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"page" text NOT NULL,
	"version" integer NOT NULL,
	"body" jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"created_by" uuid,
	"note" text DEFAULT '' NOT NULL
);
--> statement-breakpoint
ALTER TABLE "page_layout_pointers" ADD CONSTRAINT "page_layout_pointers_live_version_id_page_layouts_id_fk" FOREIGN KEY ("live_version_id") REFERENCES "public"."page_layouts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "page_layout_pointers" ADD CONSTRAINT "page_layout_pointers_draft_updated_by_users_id_fk" FOREIGN KEY ("draft_updated_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "page_layout_pointers" ADD CONSTRAINT "page_layout_pointers_scheduled_version_id_page_layouts_id_fk" FOREIGN KEY ("scheduled_version_id") REFERENCES "public"."page_layouts"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "page_layouts" ADD CONSTRAINT "page_layouts_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "page_layouts_page_version_uk" ON "page_layouts" USING btree ("page","version");--> statement-breakpoint
CREATE INDEX "page_layouts_page_idx" ON "page_layouts" USING btree ("page");
-- Seed default home layout (idempotent)
INSERT INTO page_layouts (id, page, version, body, note)
VALUES (
  '00000000-0000-4000-8000-0000000000aa',
  'home',
  1,
  '{"mobile":[{"instanceId":"00000000-0000-4000-8000-000000000001","type":"deal-row:auto-scroll","config":{"title":{"he":"בשבילך","en":"For You"},"limit":10,"speedSeconds":135},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000002","type":"deal-row:near-you","config":{"title":{"he":"לידך","en":"Near You"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000003","type":"deal-row:hot-deals","config":{"title":{"he":"עסקאות חמות","en":"Hot Deals"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000004","type":"deal-row:group-deals","config":{"title":{"he":"עסקאות קבוצתיות","en":"Group Deals"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000005","type":"deal-row:whats-left","config":{"title":{"he":"מה שנשאר","en":"What''s Left"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000010","type":"deal-row:category","config":{"title":{"he":"ארוחה","en":"Meal"},"category":"MEAL","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000011","type":"deal-row:category","config":{"title":{"he":"מאפייה","en":"Bakery"},"category":"BAKERY","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000012","type":"deal-row:category","config":{"title":{"he":"שתייה","en":"Beverage"},"category":"BEVERAGE","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000013","type":"deal-row:category","config":{"title":{"he":"קינוח","en":"Dessert"},"category":"DESSERT","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000014","type":"deal-row:category","config":{"title":{"he":"ארוז","en":"Packaged"},"category":"PACKAGED","limit":10},"visibility":{"guests":true,"loggedIn":true}}],"desktop":[{"instanceId":"00000000-0000-4000-8000-000000000001","type":"deal-row:auto-scroll","config":{"title":{"he":"בשבילך","en":"For You"},"limit":10,"speedSeconds":135},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000002","type":"deal-row:near-you","config":{"title":{"he":"לידך","en":"Near You"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000003","type":"deal-row:hot-deals","config":{"title":{"he":"עסקאות חמות","en":"Hot Deals"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000004","type":"deal-row:group-deals","config":{"title":{"he":"עסקאות קבוצתיות","en":"Group Deals"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000005","type":"deal-row:whats-left","config":{"title":{"he":"מה שנשאר","en":"What''s Left"},"limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000010","type":"deal-row:category","config":{"title":{"he":"ארוחה","en":"Meal"},"category":"MEAL","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000011","type":"deal-row:category","config":{"title":{"he":"מאפייה","en":"Bakery"},"category":"BAKERY","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000012","type":"deal-row:category","config":{"title":{"he":"שתייה","en":"Beverage"},"category":"BEVERAGE","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000013","type":"deal-row:category","config":{"title":{"he":"קינוח","en":"Dessert"},"category":"DESSERT","limit":10},"visibility":{"guests":true,"loggedIn":true}},{"instanceId":"00000000-0000-4000-8000-000000000014","type":"deal-row:category","config":{"title":{"he":"ארוז","en":"Packaged"},"category":"PACKAGED","limit":10},"visibility":{"guests":true,"loggedIn":true}}]}'::jsonb,
  'Seed: matches pre-organizer Feed order'
)
ON CONFLICT (id) DO NOTHING;

INSERT INTO page_layout_pointers (page, live_version_id)
VALUES ('home', '00000000-0000-4000-8000-0000000000aa')
ON CONFLICT (page) DO NOTHING;
