CREATE TABLE `addresses` (
  `id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
  `name` text,
  `localpart` text NOT NULL,
  `domain` text NOT NULL,
  `email` text NOT NULL
);

CREATE TABLE `mailboxes` (
  `id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  `uidnext` int NOT NULL DEFAULT 1,
  `nextmodseq` bigint NOT NULL DEFAULT 1,
  `first_recent` int NOT NULL DEFAULT 1,
  `uidvalidity` int NOT NULL DEFAULT 1
);

CREATE TABLE `messages` (
  `id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
  `idate` timestamp NOT NULL,
  `rfc822size` int
);

CREATE TABLE `mailbox_messages` (
  `mailbox` int NOT NULL,
  `uid` int NOT NULL,
  `message` int NOT NULL,
  `modseq` bigint NOT NULL,
  `seen` boolean NOT NULL DEFAULT false,
  `deleted` boolean NOT NULL DEFAULT false,
  PRIMARY KEY (`uid`, `message`)
);

CREATE TABLE `bodyparts` (
  `id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
  `bytes` int NOT NULL,
  `hash` text NOT NULL,
  `text` text,
  `data` binary
);

CREATE TABLE `part_numbers` (
  `message` int PRIMARY KEY NOT NULL,
  `part` text NOT NULL,
  `bodypart` int NOT NULL,
  `bytes` int,
  `nb_lines` int
);

CREATE TABLE `field_names` (
  `id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL
);

CREATE TABLE `header_fields` (
  `id` int NOT NULL AUTO_INCREMENT,
  `message` int NOT NULL,
  `part` text NOT NULL,
  `position` int NOT NULL,
  `field` int NOT NULL,
  `value` text,
  PRIMARY KEY (`id`, `message`)
);

CREATE TABLE `address_fields` (
  `message` int NOT NULL,
  `part` text NOT NULL,
  `position` int NOT NULL,
  `field` int NOT NULL,
  `number` int,
  `address` int NOT NULL
);

CREATE UNIQUE INDEX `addresses_index_0` ON `addresses` (`email`);

CREATE UNIQUE INDEX `mailboxes_index_1` ON `mailboxes` (`name`);

CREATE UNIQUE INDEX `field_names_index_2` ON `field_names` (`name`);

CREATE UNIQUE INDEX `header_fields_index_3` ON `header_fields` (`message`);

CREATE UNIQUE INDEX `header_fields_index_4` ON `header_fields` (`part`);

CREATE UNIQUE INDEX `header_fields_index_5` ON `header_fields` (`position`);

CREATE UNIQUE INDEX `header_fields_index_6` ON `header_fields` (`field`);

ALTER TABLE `mailbox_messages` ADD FOREIGN KEY (`mailbox`) REFERENCES `mailboxes` (`id`);

ALTER TABLE `messages` ADD FOREIGN KEY (`id`) REFERENCES `mailbox_messages` (`message`);

ALTER TABLE `part_numbers` ADD FOREIGN KEY (`message`) REFERENCES `messages` (`id`);

ALTER TABLE `bodyparts` ADD FOREIGN KEY (`id`) REFERENCES `part_numbers` (`bodypart`);

ALTER TABLE `header_fields` ADD FOREIGN KEY (`message`) REFERENCES `messages` (`id`);

ALTER TABLE `header_fields` ADD FOREIGN KEY (`part`) REFERENCES `part_numbers` (`part`);

ALTER TABLE `header_fields` ADD FOREIGN KEY (`field`) REFERENCES `field_names` (`id`);

ALTER TABLE `address_fields` ADD FOREIGN KEY (`message`) REFERENCES `messages` (`id`);

ALTER TABLE `address_fields` ADD FOREIGN KEY (`part`) REFERENCES `part_numbers` (`part`);

ALTER TABLE `address_fields` ADD FOREIGN KEY (`field`) REFERENCES `field_names` (`id`);

ALTER TABLE `address_fields` ADD FOREIGN KEY (`address`) REFERENCES `addresses` (`id`);
