-- Mail storage

-- 1
CREATE TABLE address (
  address_id      INT AUTO_INCREMENT,
  address_name    TEXT,
  localpart       TEXT NOT NULL,
  domain          TEXT NOT NULL,
  email           TEXT NOT NULL,
  PRIMARY KEY (address_id),
  UNIQUE KEY (email)
);

-- 2 app
CREATE TABLE app_account (
  account_id      INT AUTO_INCREMENT,
  user_id         INT NOT NULL,
  account_pwd     BINARY(22),
  xoauth          VARCHAR(116),
  xoauth2         VARCHAR(116),
  host            VARCHAR(255) NOT NULL DEFAULT 'localhost',
  port            INT(5) NOT NULL DEFAULT 143,
  tls             BOOLEAN NOT NULL DEFAULT true,
  PRIMARY KEY (account_id),
  FOREIGN KEY (user_id) REFERENCES address(address_id) ON DELETE CASCADE
);

-- 3
CREATE TABLE mailbox (
  mailbox_id      INT AUTO_INCREMENT,
  account_id      INT NOT NULL,
  mailbox_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,
  PRIMARY KEY (mailbox_id),
  FOREIGN KEY (account_id) REFERENCES app_account(account_id) ON DELETE CASCADE
);

-- 4
CREATE TABLE message (
  message_id      INT AUTO_INCREMENT,
  messageID       TEXT NOT NULL,
  idate           TIMESTAMP NOT NULL,
  rfc822size      INT NOT NULL,
  PRIMARY KEY (message_id),
  UNIQUE KEY (messageID)
);

-- 5
-- if mailbox_message deleted message is not deleted
CREATE TABLE mailbox_message (
  mailbox_id      INT NOT NULL,
  uid             INT,
  message_id      INT,
  modseq          BIGINT NOT NULL,
  seen            BOOLEAN NOT NULL DEFAULT false,
  deleted         BOOLEAN NOT NULL DEFAULT false,
  PRIMARY KEY (uid, message_id),
  FOREIGN KEY (mailbox_id) REFERENCES mailbox(mailbox_id) ON DELETE CASCADE,
  FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE
);

-- 6
CREATE TABLE bodypart (
  bodypart_id   INT AUTO_INCREMENT,
  bytes         INT NOT NULL,
  hash          TEXT NOT NULL,
  text          TEXT,
  data          BINARY,
  PRIMARY KEY (bodypart_id)
);

-- 7
CREATE TABLE source (
  message_id    INT NOT NULL,
  content       TEXT NOT NULL,
  PRIMARY KEY (message_id),
  FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE
);

-- 8
CREATE TABLE field_name (
  field_id      INT AUTO_INCREMENT,
  field_name    VARCHAR (255),
  PRIMARY KEY (field_id),
  UNIQUE KEY (field_name)
);

-- 9
CREATE TABLE header_field (
  message_id      INT NOT NULL,
  field_id        INT NOT NULL,
  bodypart_id     INT,
  part            VARCHAR(128),
  value           TEXT,
  UNIQUE KEY (message_id, field_id, bodypart_id),
  UNIQUE KEY (message_id, field_id, part),
  FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE,
  FOREIGN KEY (field_id) REFERENCES field_name(field_id) ON DELETE CASCADE,
  FOREIGN KEY (bodypart_id) REFERENCES bodypart(bodypart_id)
);

-- 10
CREATE TABLE address_field (
  message_id      INT NOT NULL,
  field_id        INT NOT NULL,
  address_id      INT NOT NULL,
  number          INT,
  UNIQUE KEY (message_id, field_id, address_id),
  FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE,
  FOREIGN KEY (field_id) REFERENCES field_name(field_id),
  FOREIGN KEY (address_id) REFERENCES address(address_id)
);

-- App table

-- 11
CREATE TABLE app_room (
  room_id         INT AUTO_INCREMENT,
  room_name       VARCHAR(255) NOT NULL,
  owner_id        INT NOT NULL,
  message_id      INT NOT NULL,
  room_type       INT NOT NULL DEFAULT 0,
  notSeen         INT NOT NULL DEFAULT 0,
  lastUpdate      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  PRIMARY KEY (room_id),
  UNIQUE KEY (owner_id, message_id, room_type),
  FOREIGN KEY (owner_id) REFERENCES address(address_id),
  FOREIGN KEY (message_id) REFERENCES message(message_id)
);

-- 12
CREATE TABLE app_thread (
  room_id         INT NOT NULL,
  parent_id  INT,
  root_id    INT,
  PRIMARY KEY (room_id),
  UNIQUE KEY (room_id, parent_id, root_id),
  FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE CASCADE,
  FOREIGN KEY (parent_id) REFERENCES app_room(room_id) ON DELETE SET NULL,
  FOREIGN KEY (root_id) REFERENCES app_room(room_id) ON DELETE SET NULL
);

-- 13
CREATE TABLE app_room_message (
  message_id      INT NOT NULL,
  room_id         INT,
  UNIQUE KEY (message_id, room_id),
  FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE,
  FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE SET NULL
);

-- 14
-- todo needed ?
CREATE TABLE app_room_member (
  room_id         INT NOT NULL,
  member_id       INT NOT NULL,
  UNIQUE KEY (room_id, member_id),
  FOREIGN KEY (room_id) REFERENCES app_room(room_id) ON DELETE CASCADE,
  FOREIGN KEY (member_id) REFERENCES address(address_id)
);

-- 15
create table flag_name (
  flag_id          INT AUTO_INCREMENT,
  flag_name        VARCHAR(255) NOT NULL,
  PRIMARY KEY (flag_id),
  UNIQUE KEY (flag_name)
);

-- 16
create table flag (
  message_id        INT NOT NULL,
  flag_id           INT NOT NULL,
  UNIQUE KEY (message_id, flag_id),
  FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE,
  FOREIGN KEY (message_id) REFERENCES message(message_id) ON DELETE CASCADE,
  FOREIGN KEY (flag_id) REFERENCES flag_name(flag_id) ON DELETE CASCADE
);