create type public.thread_kind as enum
  ( 'feed'
  , 'message_feed'
  , 'message'
  , 'post_feed'
  , 'post'
  , 'comment'
  , 'symbolic'
  );

create table public.thread
  ( id        int                not null primary key generated always as identity
  , uid       uuid               not null default gen_random_uuid()
  , deleted   boolean            not null default false
  , kind      public.thread_kind not null
  );

select audit('public', 'thread', array[] :: audited_column[], soft_delete => true);
select immutable('public', 'thread', array['id', 'uid', 'kind']);

create table public.thread_feed
  ( id        int     not null primary key generated always as identity
  , uid       uuid    not null default gen_random_uuid()
  , deleted   boolean not null default false
  , thread    int     not null references public.thread(id)
  -- , community int     not null references public.community(id)
  );

select audit('public', 'thread_feed', array[] :: audited_column[], soft_delete => true);
select immutable('public', 'thread_feed', array['id', 'uid', 'thread', 'community']);

create type public.thread_attachment_kind as enum 
  ( 'vote'
  , 'emoji'
  );

create type public.thread_attachment_vote_direction as enum 
  ( 'up'
  , 'down'
  );

create table public.thread_attachment
  ( id     int                           not null primary key generated always as identity
  , uid    uuid                          not null default gen_random_uuid()
  , thread int                           not null references public.thread(id)
  , kind   public.thread_attachment_kind not null
  );

select audit('public', 'thread_attachment', array[] :: audited_column[], soft_delete => true);
select immutable('public', 'thread_attachment', array['id', 'uid', 'thread', 'kind']);

create table public.thread_attachment_emoji
  ( id                int  not null primary key generated always as identity
  , uid               uuid not null default gen_random_uuid()
  , thread_attachment int  not null references public.thread_attachment(id)
  , emoji             text not null
  );

select audit('public', 'thread_attachment_emoji', array[] :: audited_column[], soft_delete => true);
select immutable('public', 'thread_attachment_emoji', array['id', 'uid', 'thread_attachment', 'emoji']);

create table public.thread_attachment_vote
  ( id                int                                     not null primary key generated always as identity
  , uid               uuid                                    not null default gen_random_uuid()
  , thread_attachment int                                     not null references public.thread_attachment(id)
  , direction         public.thread_attachment_vote_direction not null
  );

select audit('public', 'thread_attachment_vote', array[] :: audited_column[], soft_delete => true);
select immutable('public', 'thread_attachment_vote', array['id', 'uid', 'thread_attachment', 'direction']);
