CREATE DATABASE `bremen_calling` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;

CREATE TABLE `participant` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(128) DEFAULT NULL,
  `street` varchar(128) DEFAULT NULL,
  `postal_code` varchar(5) DEFAULT NULL,
  `city` varchar(64) DEFAULT NULL,
  `flags` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='An organization taking part';


CREATE TABLE `ship` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `imo` int(11) DEFAULT NULL,
  `callsign` varchar(8) DEFAULT NULL,
  `length` FLOAT NULL DEFAULT NULL,
	`width` FLOAT NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


CREATE TABLE `shipcall` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`ship_id` INT(11) NULL DEFAULT NULL,
	`type` TINYINT(4) NULL DEFAULT NULL,
	`eta` DATETIME NULL DEFAULT NULL,
	`voyage` VARCHAR(16) NULL DEFAULT NULL,
	`etd` DATETIME NULL DEFAULT NULL,
	`arrival_berth_id` INT(10) NULL DEFAULT NULL,
	`departure_berth_id` INT(10) NULL DEFAULT NULL,
	`tug_required` BIT(1) NULL DEFAULT NULL,
	`pilot_required` BIT(1) NULL DEFAULT NULL,
	`flags` INT(10) UNSIGNED NULL DEFAULT 0,
	`pier_side` BIT(1) NULL DEFAULT NULL,
	`bunkering` BIT(1) NULL DEFAULT NULL,
	`replenishing` BIT(1) NULL DEFAULT NULL,
	`draft` FLOAT NULL DEFAULT NULL,
	`tidal_window_from` DATETIME NULL DEFAULT NULL,
	`tidal_window_tp` DATETIME NULL DEFAULT NULL,
	`rain_sensitive_cargo` BIT(1) NULL DEFAULT b'0',
	`recommended_tugs` INT(11) NULL DEFAULT 0,
	PRIMARY KEY (`id`)
)
COMMENT='Incoming, outgoing or moving to another berth'
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `times` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `start_planned` datetime DEFAULT NULL,
  `end_planned` datetime DEFAULT NULL,
  `duration_planned` int(11) DEFAULT NULL,
  `start_actual` datetime DEFAULT NULL,
  `end_actual` datetime DEFAULT NULL,
  `duration_actual` int(11) DEFAULT NULL,
  `shipcall_id` int(11) DEFAULT NULL,
  `participant_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='the planned time for the participants work';


CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `participant_id` int(11) DEFAULT NULL,
  `first_name` varchar(45) DEFAULT NULL,
  `last_name` varchar(45) DEFAULT NULL,
  `user_name` varchar(45) DEFAULT NULL,
  `password_hash` varchar(128) DEFAULT NULL,
  `api_key` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='member of a participant';


CREATE TABLE `participant` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(128) NULL DEFAULT NULL,
	`street` VARCHAR(128) NULL DEFAULT NULL,
	`postal_code` VARCHAR(5) NULL DEFAULT NULL,
	`city` VARCHAR(64) NULL DEFAULT NULL,
	`roles` INT(10) UNSIGNED NULL DEFAULT 0 COMMENT 'Bitarray of assigned roles',
	`flags` INT(10) UNSIGNED NULL DEFAULT NULL,
	PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='An organization taking part';


CREATE TABLE `berth` (
	`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
	`name` VARCHAR(128) NULL DEFAULT NULL COMMENT 'Descriptive name',
	`participant_id` INT(10) UNSIGNED NULL DEFAULT NULL COMMENT 'If berth belongs to a participant, reference it here',
	`lock` BIT(1) NULL DEFAULT NULL COMMENT 'The lock must be used',
	PRIMARY KEY (`id`)
)
COMMENT='Berth of ship for a ship call'
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
;


CREATE TABLE `shipcall_participant_map` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `shipcall_id` int(10) unsigned DEFAULT NULL,
  `participant_id` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Associates a participant with a shipcall';


CREATE TABLE `shipcall_tug_map` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`shipcall_id` INT(11) NOT NULL COMMENT 'Ref to ship call',
	`ship_id` INT(11) NOT NULL COMMENT 'Ref to ship (that is a tug)',
	PRIMARY KEY (`id`)
)
COMMENT='Mapping table that assigns tugs to a ship call'
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
;
