-- AppView database migration 0003
-- Adds embed + thread-context columns to `posts`.
--
-- Why
--
-- Phase 4 (indexer) only stored `text` + `parent_uri` + `root_uri`, which
-- was enough to render plain-text timelines. Phase 5 wants embeds (images,
-- link cards, quoted posts) and thread context visible in the UI.
--
-- Columns
--   embed                 JSONB, nullable — the full AT-Protocol embed
--                         object as it appears in the record value. We
--                         store the whole thing verbatim rather than
--                         normalising into a separate `embeds` table so
--                         the UI can decode it without a second round
--                         trip, and so a future lexicon change doesn't
--                         require a schema migration.
--   reply_parent_handle   TEXT, nullable — display handle for
--                         `parent_uri`'s author. Backfilled by the
--                         handle-sync worker. Nullable because the
--                         sync worker hasn't seen the row yet, OR
--                         because the parent isn't in our index.
--   reply_root_handle     TEXT, nullable — display handle for
--                         `root_uri`'s author. Same semantics.
--   reply_root_uri        TEXT, nullable — duplicate of `root_uri`
--                         for the "show full thread" link target.
--                         Kept as its own column so the index covers
--                         it without having to special-case NULLs on
--                         the existing `root_uri`.
--
-- All columns are nullable. Pre-existing rows will continue to render
-- correctly — old posts simply have `embed = NULL` and the UI omits the
-- embed block.

ALTER TABLE posts ADD COLUMN IF NOT EXISTS embed JSONB;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS reply_parent_handle TEXT;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS reply_root_handle TEXT;
ALTER TABLE posts ADD COLUMN IF NOT EXISTS reply_root_uri TEXT;