-- Migration: add FAQ fields to home_page global
-- Scalar faq_title goes on the main table; faq_items is an array child table.
-- The versions table (_home_page_v) mirrors these fields with version_ prefix.

-- 1. Main table: scalar column for faq title
ALTER TABLE home_page ADD COLUMN IF NOT EXISTS faq_title character varying;

-- 2. Main table: array child table for faq items
-- IMPORTANT: id must be character varying (not serial) — Payload generates string IDs
CREATE TABLE IF NOT EXISTS home_page_faq_items (
  _order integer NOT NULL,
  _parent_id integer NOT NULL REFERENCES home_page(id) ON DELETE CASCADE,
  id character varying NOT NULL,
  question character varying,
  answer text,
  updated_at timestamp with time zone DEFAULT now(),
  created_at timestamp with time zone DEFAULT now(),
  CONSTRAINT home_page_faq_items_pkey PRIMARY KEY (id)
);
CREATE INDEX IF NOT EXISTS home_page_faq_items_order_idx ON home_page_faq_items (_order);
CREATE INDEX IF NOT EXISTS home_page_faq_items_parent_id_idx ON home_page_faq_items (_parent_id);

-- 3. Versions table: scalar column mirror
ALTER TABLE _home_page_v ADD COLUMN IF NOT EXISTS version_faq_title character varying;

-- 4. Versions table: array child table mirror (uses serial id, _uuid, no timestamps)
CREATE TABLE IF NOT EXISTS _home_page_v_version_faq_items (
  _order integer NOT NULL,
  _parent_id integer NOT NULL REFERENCES _home_page_v(id) ON DELETE CASCADE,
  id serial PRIMARY KEY,
  question character varying,
  answer text,
  _uuid character varying
);
CREATE INDEX IF NOT EXISTS _home_page_v_version_faq_items_order_idx ON _home_page_v_version_faq_items (_order);
CREATE INDEX IF NOT EXISTS _home_page_v_version_faq_items_parent_id_idx ON _home_page_v_version_faq_items (_parent_id);
