-- Migration: 0006 — DinosaurPage global + dynozavry location
-- Idempotent: uses IF NOT EXISTS / ON CONFLICT DO NOTHING

-- ─── Main global table ────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS "dinosaur_page" (
  "id"                      integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
  "hero_title"              varchar,
  "hero_description"        varchar,
  "hero_stat"               varchar,
  "hero_stat_label"         varchar,
  "hero_image_id"           integer REFERENCES "media"("id") ON DELETE SET NULL,
  "activities_title"        varchar,
  "activities_description"  varchar,
  "why_visit_title"         varchar,
  "working_hours"           varchar,
  "combo_description"       varchar,
  "updated_at"              timestamp(3) with time zone DEFAULT now() NOT NULL,
  "created_at"              timestamp(3) with time zone DEFAULT now() NOT NULL
);

-- ─── hero_features array ──────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS "dinosaur_page_hero_features" (
  "_order"     integer NOT NULL,
  "_parent_id" integer NOT NULL REFERENCES "dinosaur_page"("id") ON DELETE CASCADE,
  "id"         varchar PRIMARY KEY NOT NULL,
  "text"       varchar
);
CREATE INDEX IF NOT EXISTS "dinosaur_page_hero_features_order_idx"    ON "dinosaur_page_hero_features" ("_order");
CREATE INDEX IF NOT EXISTS "dinosaur_page_hero_features_parent_idx"   ON "dinosaur_page_hero_features" ("_parent_id");

-- ─── dinosaurs array (wheel) ──────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS "dinosaur_page_dinosaurs" (
  "_order"               integer NOT NULL,
  "_parent_id"           integer NOT NULL REFERENCES "dinosaur_page"("id") ON DELETE CASCADE,
  "id"                   varchar PRIMARY KEY NOT NULL,
  "name"                 varchar,
  "epoch"                varchar,
  "length"               varchar,
  "weight"               varchar,
  "image_id"             integer REFERENCES "media"("id") ON DELETE SET NULL,
  "thumbnail_image_id"   integer REFERENCES "media"("id") ON DELETE SET NULL
);
CREATE INDEX IF NOT EXISTS "dinosaur_page_dinosaurs_order_idx"   ON "dinosaur_page_dinosaurs" ("_order");
CREATE INDEX IF NOT EXISTS "dinosaur_page_dinosaurs_parent_idx"  ON "dinosaur_page_dinosaurs" ("_parent_id");

-- ─── gallery_images array ─────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS "dinosaur_page_gallery_images" (
  "_order"     integer NOT NULL,
  "_parent_id" integer NOT NULL REFERENCES "dinosaur_page"("id") ON DELETE CASCADE,
  "id"         varchar PRIMARY KEY NOT NULL,
  "image_id"   integer REFERENCES "media"("id") ON DELETE SET NULL
);
CREATE INDEX IF NOT EXISTS "dinosaur_page_gallery_images_order_idx"   ON "dinosaur_page_gallery_images" ("_order");
CREATE INDEX IF NOT EXISTS "dinosaur_page_gallery_images_parent_idx"  ON "dinosaur_page_gallery_images" ("_parent_id");

-- ─── activities array ─────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS "dinosaur_page_activities" (
  "_order"      integer NOT NULL,
  "_parent_id"  integer NOT NULL REFERENCES "dinosaur_page"("id") ON DELETE CASCADE,
  "id"          varchar PRIMARY KEY NOT NULL,
  "name"        varchar,
  "price"       varchar,
  "description" varchar,
  "image_id"    integer REFERENCES "media"("id") ON DELETE SET NULL,
  "href"        varchar
);
CREATE INDEX IF NOT EXISTS "dinosaur_page_activities_order_idx"   ON "dinosaur_page_activities" ("_order");
CREATE INDEX IF NOT EXISTS "dinosaur_page_activities_parent_idx"  ON "dinosaur_page_activities" ("_parent_id");

-- ─── why_visit_items array ────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS "dinosaur_page_why_visit_items" (
  "_order"      integer NOT NULL,
  "_parent_id"  integer NOT NULL REFERENCES "dinosaur_page"("id") ON DELETE CASCADE,
  "id"          varchar PRIMARY KEY NOT NULL,
  "title"       varchar,
  "description" varchar
);
CREATE INDEX IF NOT EXISTS "dinosaur_page_why_visit_items_order_idx"   ON "dinosaur_page_why_visit_items" ("_order");
CREATE INDEX IF NOT EXISTS "dinosaur_page_why_visit_items_parent_idx"  ON "dinosaur_page_why_visit_items" ("_parent_id");

-- ─── review_videos array ─────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS "dinosaur_page_review_videos" (
  "_order"     integer NOT NULL,
  "_parent_id" integer NOT NULL REFERENCES "dinosaur_page"("id") ON DELETE CASCADE,
  "id"         varchar PRIMARY KEY NOT NULL,
  "src"        varchar,
  "poster"     varchar,
  "label"      varchar
);
CREATE INDEX IF NOT EXISTS "dinosaur_page_review_videos_order_idx"   ON "dinosaur_page_review_videos" ("_order");
CREATE INDEX IF NOT EXISTS "dinosaur_page_review_videos_parent_idx"  ON "dinosaur_page_review_videos" ("_parent_id");

-- ─── Seed initial dinosaur_page row (so findGlobal works before seed) ─────────
INSERT INTO "dinosaur_page" (id, hero_title, working_hours)
VALUES (1, 'Динопарк — портал у світ динозаврів', 'п''ятниця-субота-неділя з 11:00 до 20:00')
ON CONFLICT (id) DO NOTHING;

-- ─── Location record for dynozavry ───────────────────────────────────────────
INSERT INTO "locations" (
  name, slug, tagline, short_desc,
  show_in_menu, show_on_home, show_detail_page, sort
)
VALUES (
  'Динопарк',
  'dynozavry',
  'Портал у світ динозаврів',
  'Великі динозаври, що рухаються та гарчать. 26 унікальних експонатів.',
  true, true, false, 20
)
ON CONFLICT (slug) DO NOTHING;

-- SEO plugin meta columns (added by seoPlugin for globals)
ALTER TABLE "dinosaur_page" ADD COLUMN IF NOT EXISTS "meta_title" varchar;
ALTER TABLE "dinosaur_page" ADD COLUMN IF NOT EXISTS "meta_description" varchar;
ALTER TABLE "dinosaur_page" ADD COLUMN IF NOT EXISTS "meta_image_id" integer REFERENCES "media"("id") ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS "dinosaur_page_meta_meta_image_idx" ON "dinosaur_page" ("meta_image_id");
