# Drop dependent tables first (reverse dependency order) before dropping report
DROP TABLE IF EXISTS `report_record_spf`;
DROP TABLE IF EXISTS `report_record_dkim`;
DROP TABLE IF EXISTS `report_record_reason`;
DROP TABLE IF EXISTS `report_record`;
DROP TABLE IF EXISTS `report_policy_published`;
DROP TABLE IF EXISTS `report_error`;
DROP TABLE IF EXISTS `report`;
DROP TABLE IF EXISTS `fk_spf_scope`;
DROP TABLE IF EXISTS `fk_spf_result`;
DROP TABLE IF EXISTS `fk_dkim_result`;
DROP TABLE IF EXISTS `fk_disposition_reason`;
DROP TABLE IF EXISTS `fk_disposition`;
DROP TABLE IF EXISTS `domain`;
DROP TABLE IF EXISTS `author`;


# Dump of table author
# ------------------------------------------------------------

CREATE TABLE `author` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `org_name` varchar(253) NOT NULL DEFAULT '',
  `email`    varchar(255) DEFAULT NULL,
  `extra_contact` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;



# Dump of table domain
# ------------------------------------------------------------

CREATE TABLE `domain` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `domain` varchar(253) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `domain` (`domain`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;


# Dump of table report
# ------------------------------------------------------------

CREATE TABLE `report` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `begin` int(11) unsigned NOT NULL,
  `end` int(11) unsigned NOT NULL,
  `author_id` int(11) unsigned NOT NULL,
  `rcpt_domain_id` int(11) unsigned DEFAULT NULL,
  `from_domain_id` int(11) unsigned NOT NULL,
  `uuid` varchar(253) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `author_id` (`author_id`),
  KEY `from_domain_id` (`from_domain_id`),
  CONSTRAINT `report_ibfk_3` FOREIGN KEY (`from_domain_id`) REFERENCES `domain` (`id`) ON UPDATE CASCADE,
  CONSTRAINT `report_ibfk_1` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=ascii;


CREATE TABLE `report_error` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `report_id` int(11) unsigned NOT NULL,
  `error` varchar(255) NOT NULL DEFAULT '',
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `report_id` (`report_id`),
  CONSTRAINT `report_error_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;


# Dump of table fk_disposition
# ------------------------------------------------------------

CREATE TABLE `fk_disposition` (
  `disposition` varchar(10) NOT NULL DEFAULT '',
  PRIMARY KEY (`disposition`)
) ENGINE=InnoDB DEFAULT CHARSET=ascii;

LOCK TABLES `fk_disposition` WRITE;
/*!40000 ALTER TABLE `fk_disposition` DISABLE KEYS */;

INSERT INTO `fk_disposition` (`disposition`)
VALUES
	('none'),
	('quarantine'),
	('reject');

/*!40000 ALTER TABLE `fk_disposition` ENABLE KEYS */;
UNLOCK TABLES;


# Dump of table fk_disposition_reason
# ------------------------------------------------------------

CREATE TABLE `fk_disposition_reason` (
  `type` varchar(24) NOT NULL DEFAULT '',
  PRIMARY KEY (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=ascii;

LOCK TABLES `fk_disposition_reason` WRITE;
/*!40000 ALTER TABLE `fk_disposition_reason` DISABLE KEYS */;

INSERT INTO `fk_disposition_reason` (`type`)
VALUES
	('forwarded'),
	('local_policy'),
	('mailing_list'),
	('other'),
	('sampled_out'),
	('trusted_forwarder');

/*!40000 ALTER TABLE `fk_disposition_reason` ENABLE KEYS */;
UNLOCK TABLES;


# Dump of table fk_dkim_result
# ------------------------------------------------------------

CREATE TABLE `fk_dkim_result` (
  `result` varchar(9) NOT NULL DEFAULT '',
  PRIMARY KEY (`result`)
) ENGINE=InnoDB DEFAULT CHARSET=ascii;

LOCK TABLES `fk_dkim_result` WRITE;
/*!40000 ALTER TABLE `fk_dkim_result` DISABLE KEYS */;

INSERT INTO `fk_dkim_result` (`result`)
VALUES
	('fail'),
	('neutral'),
	('none'),
	('pass'),
	('permerror'),
	('policy'),
	('temperror');

/*!40000 ALTER TABLE `fk_dkim_result` ENABLE KEYS */;
UNLOCK TABLES;


# Dump of table fk_spf_result
# ------------------------------------------------------------

CREATE TABLE `fk_spf_result` (
  `result` varchar(9) NOT NULL DEFAULT '',
  PRIMARY KEY (`result`)
) ENGINE=InnoDB DEFAULT CHARSET=ascii;

LOCK TABLES `fk_spf_result` WRITE;
/*!40000 ALTER TABLE `fk_spf_result` DISABLE KEYS */;

INSERT INTO `fk_spf_result` (`result`)
VALUES
	('fail'),
	('neutral'),
	('none'),
	('pass'),
	('permerror'),
	('softfail'),
	('temperror');

/*!40000 ALTER TABLE `fk_spf_result` ENABLE KEYS */;
UNLOCK TABLES;


# Dump of table fk_spf_scope
# ------------------------------------------------------------

CREATE TABLE `fk_spf_scope` (
  `scope` varchar(5) NOT NULL DEFAULT '',
  PRIMARY KEY (`scope`)
) ENGINE=InnoDB DEFAULT CHARSET=ascii;

LOCK TABLES `fk_spf_scope` WRITE;
/*!40000 ALTER TABLE `fk_spf_scope` DISABLE KEYS */;

INSERT INTO `fk_spf_scope` (`scope`)
VALUES
	('helo'),
	('mfrom');

/*!40000 ALTER TABLE `fk_spf_scope` ENABLE KEYS */;
UNLOCK TABLES;


# Dump of table report_policy_published
# ------------------------------------------------------------

CREATE TABLE `report_policy_published` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `report_id` int(11) unsigned NOT NULL,
  `adkim` char(1) DEFAULT NULL,
  `aspf` char(1) DEFAULT NULL,
  `p` varchar(10) DEFAULT NULL,
  `sp` varchar(10) DEFAULT NULL,
  `pct` tinyint(1) unsigned DEFAULT NULL,
  `rua` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `report_id` (`report_id`),
  CONSTRAINT `report_policy_published_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=ascii;



# Dump of table report_record
# ------------------------------------------------------------

CREATE TABLE `report_record` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `report_id` int(11) unsigned NOT NULL,
  `source_ip` varbinary(16) NOT NULL DEFAULT '',
  `count` int(11) unsigned DEFAULT NULL,
  `disposition` varchar(10) NOT NULL DEFAULT '',
  `dkim` char(4) NOT NULL DEFAULT '',
  `spf` char(4) NOT NULL DEFAULT '',
  `envelope_to_did` int(11) unsigned DEFAULT NULL,
  `envelope_from_did` int(11) unsigned DEFAULT NULL,
  `header_from_did` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `report_id` (`report_id`),
  KEY `disposition` (`disposition`),
  CONSTRAINT `report_record_ibfk_2` FOREIGN KEY (`disposition`) REFERENCES `fk_disposition` (`disposition`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `report_record_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=ascii;



# Dump of table report_record_reason
# ------------------------------------------------------------

CREATE TABLE `report_record_reason` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `report_record_id` int(11) unsigned NOT NULL,
  `type` varchar(24) NOT NULL DEFAULT '',
  `comment` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `report_record_id` (`report_record_id`),
  KEY `type` (`type`),
  CONSTRAINT `report_record_reason_ibfk_3` FOREIGN KEY (`report_record_id`) REFERENCES `report_record` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `report_record_reason_ibfk_4` FOREIGN KEY (`type`) REFERENCES `fk_disposition_reason` (`type`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=ascii;



# Dump of table report_record_dkim
# ------------------------------------------------------------

CREATE TABLE `report_record_dkim` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `report_record_id` int(11) unsigned NOT NULL,
  `domain_id` int(11) unsigned NOT NULL,
  `selector` varchar(253) DEFAULT NULL,
  `result` varchar(9) NOT NULL DEFAULT '',
  `human_result` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `report_record_id` (`report_record_id`),
  KEY `result` (`result`),
  CONSTRAINT `report_record_dkim_ibfk_2` FOREIGN KEY (`result`) REFERENCES `fk_dkim_result` (`result`),
  CONSTRAINT `report_record_dkim_ibfk_1` FOREIGN KEY (`report_record_id`) REFERENCES `report_record` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=ascii;



# Dump of table report_record_spf
# ------------------------------------------------------------

CREATE TABLE `report_record_spf` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `report_record_id` int(11) unsigned NOT NULL,
  `domain_id` int(11) unsigned NOT NULL,
  `scope` varchar(5) DEFAULT NULL,
  `result` varchar(9) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `report_record_id` (`report_record_id`),
  KEY `scope` (`scope`),
  KEY `result` (`result`),
  CONSTRAINT `report_record_spf_ibfk_1` FOREIGN KEY (`report_record_id`) REFERENCES `report_record` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `report_record_spf_ibfk_2` FOREIGN KEY (`scope`) REFERENCES `fk_spf_scope` (`scope`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `report_record_spf_ibfk_3` FOREIGN KEY (`result`) REFERENCES `fk_spf_result` (`result`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=ascii;

