create or replace procedure add_user(
    in in_username varchar(32),
    in in_password_hash varchar(72)
  )
language plpgsql
as $$
begin
  insert into "user" ("username", "password_hash")
  values (in_username, in_password_hash);

  insert into "role_binding" ("user_id", "role_id")
  values (
    (
      select "id"
      from "user"
      where "username" = in_username
    ),
    (
      select "id"
      from "role"
      where "name" = 'member'
    )
  );
end;
$$;

create or replace procedure add_channel(
    in channel_name varchar(32),
    in channel_desc varchar(200)
  )
language plpgsql
as $$
declare
  channel_id integer;
begin
  insert into "channel" ("name", "description")
  values (channel_name, channel_desc)
  returning "id" into channel_id;

  insert into "right" ("role_id", "channel_id", "rights")
  values (
    (
      select "id"
      from "role"
      where "name" = 'admin'
    ),
    channel_id,
    'A'
  );
end;
$$