-- Your SQL goes here
CREATE TABLE `messages`(
	`id` TEXT NOT NULL PRIMARY KEY,
	`text` TEXT NOT NULL,
	`sender_participant_handle` TEXT,
	`date` TIMESTAMP NOT NULL,
	`file_transfer_guids` TEXT,
	`attachment_metadata` TEXT,
	FOREIGN KEY (`sender_participant_handle`) REFERENCES `participants`(`handle`)
);

CREATE TABLE `conversation_messages`(
	`conversation_id` TEXT NOT NULL,
	`message_id` TEXT NOT NULL,
	PRIMARY KEY(`conversation_id`, `message_id`),
	FOREIGN KEY (`conversation_id`) REFERENCES `conversations`(`id`),
	FOREIGN KEY (`message_id`) REFERENCES `messages`(`id`)
);

CREATE TABLE `settings`(
	`key` TEXT NOT NULL PRIMARY KEY,
	`value` BINARY NOT NULL
);

CREATE TABLE `conversations`(
	`id` TEXT NOT NULL PRIMARY KEY,
	`unread_count` BIGINT NOT NULL,
	`display_name` TEXT,
	`last_message_preview` TEXT,
	`date` TIMESTAMP NOT NULL
);

CREATE TABLE `participants`(
	`handle` TEXT NOT NULL PRIMARY KEY,
	`is_me` BOOL NOT NULL,
	`contact_id` TEXT
);

CREATE TABLE `conversation_participants`(
	`conversation_id` TEXT NOT NULL,
	`participant_handle` TEXT NOT NULL,
	PRIMARY KEY(`conversation_id`, `participant_handle`),
	FOREIGN KEY (`conversation_id`) REFERENCES `conversations`(`id`),
	FOREIGN KEY (`participant_handle`) REFERENCES `participants`(`handle`)
);

