drop table if exists chatrooms cascade;
create table chatrooms(
	chatroom_id serial,
	chatroom_name varchar(64),
	chatroom_title varchar(64),
	ctstamp timestamp without time zone not null default now(),
	dtstamp timestamp without time zone,
	is_visible boolean default true,
	is_members_only boolean default false,
	constraint chatrooms_chatroom_id_pkey primary key( chatroom_id )
);

insert into chatrooms( chatroom_name , chatroom_title , is_visible , is_members_only ) values ( 'translators' , 'Fordítók szobája' , true , true );

comment on table chatrooms is 'storage of chatrooms';
comment on column chatrooms.chatroom_id is 'unique id of chatroom';
comment on column chatrooms.chatroom_name is 'name of the chatroom';
comment on column chatrooms.chatroom_title is 'title of the chatroom';
comment on column chatrooms.ctstamp is 'creation timestamp of chatroom';
comment on column chatrooms.dtstamp is 'deletion timestamp of chatroom';
comment on column chatrooms.is_visible is 'visible flag of of chatroom';
comment on column chatrooms.is_members_only is 'members only flag of chatroom';

comment on constraint chatrooms_chatroom_id_pkey on chatrooms is 'unique constraint of table';

drop table if exists chatroom_users;
create table chatroom_users(
	chatroom_id integer not null references chatrooms( chatroom_id ),
	user_id integer not null references users( user_id ),
	mtstamp timestamp without time zone not null default now(),
	ltstamp timestamp without time zone not null default now()
);

comment on table chatroom_users is 'storage of users in a chatroom';
comment on column chatroom_users.chatroom_id is 'reference id to chatrooms table';
comment on column chatroom_users.user_id is 'reference id to users table';
comment on column chatroom_users.mtstamp is 'last keepalive timestamp of user';
comment on column chatroom_users.ltstamp is 'login timestamp of user';

comment on constraint chatroom_users_chatroom_id_fkey on chatroom_users is 'foreign constraint to chatrooms table';
comment on constraint chatroom_users_user_id_fkey on chatroom_users is 'foreign constraint to users table';

create index chatroom_users_epoch_mtstamp_ind on chatroom_users( extract( epoch from mtstamp ) );
comment on index chatroom_users_epoch_mtstamp_ind is 'epoch mtstamp index of user (for performance)';

drop table if exists chatroom_invitable_users;
create table chatroom_invitable_users(
	chatroom_id integer not null references chatrooms( chatroom_id ),
	inviter_user_id integer not null references users( user_id ),
	user_id integer not null references users( user_id ),
	ctstamp timestamp without time zone not null default now()
);

comment on table chatroom_invitable_users is 'storage of invitable users of non-visible chatrooms';
comment on column chatroom_invitable_users.chatroom_id is 'reference id to chatrooms table';
comment on column chatroom_invitable_users.inviter_user_id is 'inviter user id, reference id to users table';
comment on column chatroom_invitable_users.user_id is 'invitable user id, referenced id to users table';
comment on column chatroom_invitable_users.ctstamp is 'invitation creation';

drop table if exists chat_messages;
create table chat_messages(
	chat_message_id serial,
	chatroom_id integer references chatrooms( chatroom_id ),
	ctstamp timestamp without time zone not null default now(),
	poster_user_id integer references users( user_id ),
	target_user_id integer references users( user_id ),
	is_system boolean default false,
	message_flags integer not null default 0,
	message_text text not null,
	constraint chat_messages_chat_message_id_pkey primary key( chat_message_id )
);

comment on table chat_messages is 'storage of chat messages';
comment on column chat_messages.chat_message_id is 'unique id of table';
comment on column chat_messages.chatroom_id is 'reference id to chatrooms table';
comment on column chat_messages.ctstamp is 'time stamp of message';
comment on column chat_messages.poster_user_id is 'id of poster user (almost mandatory - except if system)';
comment on column chat_messages.target_user_id is 'id of target user (optional, filled on private message)';
comment on column chat_messages.is_system is 'system flag of message (invisible, for processing only)';
comment on column chat_messages.message_flags is 'flags of the message';
comment on column chat_messages.message_text is 'the message itself';

comment on constraint chat_messages_chat_message_id_pkey on chat_messages is 'unique constraint of table';
comment on constraint chat_messages_chatroom_id_fkey on chat_messages is 'foreign constraint to chatrooms table';
comment on constraint chat_messages_poster_user_id_fkey on chat_messages is 'foreign constraint to users table (on poster user)';
comment on constraint chat_messages_target_user_id_fkey on chat_messages is 'foreign constraint to users table (on target user)';
