-- user comments
drop table if exists user_comments;
create table user_comments(
	user_id integer references users( user_id ),
	mtstamp timestamp without time zone not null default now(),
	number_of_comments integer not null default 1,
	score_overall double precision,
	score_average double precision
);

comment on table user_comments is 'storage of user comments (blue-board)';
comment on column user_comments.user_id is 'unique id of table / reference id to users';
comment on column user_comments.mtstamp is 'last message timestamp';
comment on column user_comments.number_of_comments is 'number of comments on user';
comment on column user_comments.score_overall is 'score overall';
comment on column user_comments.score_average is 'score average';

comment on constraint user_comments_user_id_fkey on user_comments is 'foreign constraint to users table';

create index user_comments_user_id_ind on user_comments( user_id );
comment on index user_comments_user_id_ind is 'index of user_id';
-- create index user_comments_mtstamp_ind on user_comments( mtstamp );
-- comment on index user_comments_mtstamp_ind is 'index of mtstamp';
-- create index user_comments_number_of_comments_ind on user_comments( number_of_comments );
-- comment on index user_comments_number_of_comments_ind is 'index on number of comments';
-- create index user_comments_score_average_ind on user_comments( score_average );
-- comment on index user_comments_score_average_ind is 'index of score average';

-- user_comment_details
drop table if exists user_comment_details;
create table user_comment_details(
	user_comment_detail_id serial not null,
	poster_user_id integer not null references users( user_id ),
	target_user_id integer not null references users( user_id ),
	parent_user_comment_detail_id integer references user_comment_details( user_comment_detail_id ),
	ctstamp timestamp without time zone not null default now(),
	user_comment_detail text not null,
	score double precision,
	constraint user_comment_details_user_comment_detail_id_pkey primary key( user_comment_detail_id )
);

comment on table user_comment_details is 'storage of user comment details (blue-board messages)';
comment on column user_comment_details.user_comment_detail_id is 'unique id of table';
comment on column user_comment_details.poster_user_id is 'reference id to users table (on poster user)';
comment on column user_comment_details.target_user_id is 'reference id to users table (on target user)';
comment on column user_comment_details.parent_user_comment_detail_id is 'self reference id if reply (optional)';
comment on column user_comment_details.ctstamp is 'time stamp of creation';
comment on column user_comment_details.user_comment_detail is 'the comment itself';
comment on column user_comment_details.score is 'score of the message detail';

comment on constraint user_comment_details_user_comment_detail_id_pkey on user_comment_details is 'unique constraint of table';
comment on constraint user_comment_details_poster_user_id_fkey on user_comment_details is 'foreign constraint to users table (poster_user_id)';
comment on constraint user_comment_details_target_user_id_fkey on user_comment_details is 'foreign constraint to users table (target_user_id)';
comment on constraint user_comment_details_parent_user_comment_detail_id_fkey on user_comment_details is 'self constraint to user_comment_details table';

