-- public.chat_messages definition

-- Drop table

-- DROP TABLE realtime.chat_messages;

CREATE TABLE realtime.chat_messages (
	created_at timestamptz DEFAULT now() NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	message_text text NULL,
	sender_id uuid DEFAULT gen_random_uuid() NULL,
	recipient_id uuid DEFAULT gen_random_uuid() NULL,
	img_path text NULL,
	is_img bool DEFAULT false NULL,
	"read" bool DEFAULT false NULL,
	creator_name text NULL,
	creator_image text NULL,
	contact_name text NULL,
	contact_image text NULL,
	creator text NULL,
	contact text NULL,
	CONSTRAINT chat_messages_pkey PRIMARY KEY (id)
);

-- Table Triggers

create trigger update_last_message_trigger after
insert
    on
    realtime.chat_messages for each row execute function updatelastmessage();


-- public.chat_messages foreign keys

ALTER TABLE realtime.chat_messages ADD CONSTRAINT chat_messages_recipient_id_fkey FOREIGN KEY (recipient_id) REFERENCES public.chat(id);
   
   
   -- public.chat definition

-- Drop table

-- DROP TABLE public.chat;

CREATE TABLE realtime.chat (
	created_at timestamptz DEFAULT now() NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	tenant_id uuid DEFAULT gen_random_uuid() NULL,
	chat_name text NULL,
	chat_description text NULL,
	chat_img text NULL,
	chat_members _text NULL,
	last_message text NULL,
	last_message_time timestamptz NULL,
	is_chat_group bool DEFAULT false NULL,
	creator uuid NULL,
	contact uuid NULL,
	creator_name text NULL,
	creator_image text NULL,
	contact_name text NULL,
	contact_image text NULL,
	CONSTRAINT messages_pkey PRIMARY KEY (id)
);


-- public.chat foreign keys

ALTER TABLE realtime.chat ADD CONSTRAINT chat_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES public.tenant(id);



yxNiio0gNiWA0hzGvziZ88uM09X+1DOpo0weylUyfKTUggkO5Y762LQJ5uDnTL8xx0Vso4SFszPkKOwmwVSZ7U82uuGZrojyd3by3+a2jj8=