alter table public.vocabulary_units
  add column if not exists entry_id uuid references public.content_entries(id) on delete set null,
  add column if not exists content_node_id uuid references public.content_nodes(id) on delete set null,
  add column if not exists source_hash text,
  add column if not exists metadata jsonb not null default '{}'::jsonb;

alter table public.vocabulary_words
  add column if not exists entry_id uuid references public.content_entries(id) on delete set null,
  add column if not exists content_node_id uuid references public.content_nodes(id) on delete set null,
  add column if not exists source_hash text,
  add column if not exists metadata jsonb not null default '{}'::jsonb;

alter table public.handbook_subjects
  add column if not exists entry_id uuid references public.content_entries(id) on delete set null,
  add column if not exists content_node_id uuid references public.content_nodes(id) on delete set null,
  add column if not exists source_hash text;

alter table public.handbook_chapters
  add column if not exists entry_id uuid references public.content_entries(id) on delete set null,
  add column if not exists content_node_id uuid references public.content_nodes(id) on delete set null,
  add column if not exists source_hash text,
  add column if not exists metadata jsonb not null default '{}'::jsonb;

alter table public.handbook_entries
  add column if not exists entry_id uuid references public.content_entries(id) on delete set null,
  add column if not exists content_node_id uuid references public.content_nodes(id) on delete set null,
  add column if not exists source_hash text,
  add column if not exists metadata jsonb not null default '{}'::jsonb;

create index if not exists idx_vocabulary_units_navigation
  on public.vocabulary_units(tenant_id, entry_id, content_node_id, region_id, is_active, sort_order);
create index if not exists idx_vocabulary_words_navigation
  on public.vocabulary_words(tenant_id, entry_id, content_node_id, unit_id, is_active, sort_order);
create index if not exists idx_handbook_subjects_navigation
  on public.handbook_subjects(tenant_id, entry_id, content_node_id, region_id, is_active, sort_order);
create index if not exists idx_handbook_chapters_navigation
  on public.handbook_chapters(tenant_id, entry_id, content_node_id, subject_id, is_active, sort_order);
create index if not exists idx_handbook_entries_navigation
  on public.handbook_entries(tenant_id, entry_id, content_node_id, chapter_id, is_active, sort_order);
