alter table public.documents
  add column if not exists mindmap_data jsonb,
  add column if not exists raw_text text,
  add column if not exists index_status text not null default 'pending';

alter table public.documents
  alter column index_status set default 'pending';

alter table public.documents
  alter column updated_at set default timezone('utc', now());

alter table public.documents
  alter column created_at set default timezone('utc', now());

create index if not exists documents_user_updated_idx
  on public.documents (user_id, updated_at desc);

create index if not exists documents_index_status_idx
  on public.documents (index_status);

create or replace function public.set_documents_updated_at()
returns trigger
language plpgsql
security invoker
as $$
begin
  new.updated_at := timezone('utc', now());
  return new;
end;
$$;

drop trigger if exists set_documents_updated_at on public.documents;
create trigger set_documents_updated_at
before update on public.documents
for each row
execute procedure public.set_documents_updated_at();
