select create_newtype_text('public.community_tag');

create table public.community
  ( id      int                  not null primary key generated always as identity
  , uid     uuid                 not null unique default gen_random_uuid()
  , deleted boolean              not null default false
  , tag     public.community_tag not null
  );

select audit( 'public'
            , 'community'
            , array[ row('tag', 'public.community_tag')
                   ] :: audited_column[]
            , soft_delete => true
            );

select immutable( 'public'
                , 'community'
                , array[ 'id'
                       , 'uid'
                       ]
                );

create table public.community_nonmember_scope
  ( id        int                not null primary key generated always as identity
  , uid       uuid               not null unique default gen_random_uuid()
  , community int                not null references public.community (id)
  , scope     public.authz_scope not null
  , unique (community, scope)
  );

select audit( 'public'
            , 'community_nonmember_scope'
            , array[ row('community', 'int')
                   , row('scope', 'public.authz_scope')
                   ] :: audited_column[]
            , soft_delete => false
            );

select immutable( 'public'
                , 'community_nonmember_scope'
                , array[ 'id'
                       , 'uid'
                       , 'community'
                       , 'scope'
                       ]
                );

create table public.community_member_role
  ( id          int     not null primary key generated always as identity
  , uid         uuid    not null unique default gen_random_uuid()
  , community   int     not null references public.community (id)
  , title       text    not null
  , description text    not null
  );

select audit( 'public'
            , 'community_member_role'
            , array[ row('title', 'text')
                   , row('description', 'text')
                   ] :: audited_column[]
            , soft_delete => false
            );

select immutable( 'public'
                , 'community_member_role'
                , array[ 'id'
                       , 'uid'
                       , 'community'
                       ]
                );

create table public.community_member_role_scope
  ( id    int                not null primary key generated always as identity
  , uid   uuid               not null unique default gen_random_uuid()
  , role_ int                not null references public.community_member_role (id)
  , scope public.authz_scope not null
  , unique (scope, role_)
  );

select audit( 'public'
            , 'community_member_role_scope'
            , array[ row('role_', 'int')
                   , row('scope', 'public.authz_scope')
                   ] :: audited_column[]
            , soft_delete => false
            );

select immutable( 'public'
                , 'community_member_role_scope'
                , array[ 'id'
                       , 'uid'
                       , 'role_'
                       , 'scope'
                       ]
                );

create table public.community_member
  ( id        int  not null primary key generated always as identity
  , uid       uuid not null unique default gen_random_uuid()
  , usr       int  not null references public.usr (id)
  , community int  not null references public.community (id)
  , role_     int  not null references public.community_member_role (id)
  );

select audit( 'public'
            , 'community_member'
            , array[ row('usr', 'int')
                   , row('role_', 'int')
                   ] :: audited_column[]
            , soft_delete => false
            );

select immutable( 'public'
                , 'community_member'
                , array[ 'id'
                       , 'uid'
                       , 'usr'
                       ]
                );

