CREATE DATABASE `##poolctrl##` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE ##poolctrl##;
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

-- PoolCtrl Tabellen

CREATE TABLE IF NOT EXISTS `poolctrl_event` (
  `eventID` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `start` timestamp COLLATE utf8_unicode_ci NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  `end` timestamp COLLATE utf8_unicode_ci NULL,
  `participants` int(4) DEFAULT NULL,
  `note` varchar(140) COLLATE utf8_unicode_ci DEFAULT NULL,
  `category` int(11) NOT NULL,
  `pbs_poolID` int(11) NOT NULL,
  `pbs_membershipID` int(11) NOT NULL,
  `pbs_bootosID` int(11) NOT NULL,
  `pbs_bootmenuID` int(11) NOT NULL,
  `pbs_filterID` int(11) NOT NULL,
  `repeat` bool NOT NULL DEFAULT false,
  `repeatEnd` timestamp COLLATE utf8_unicode_ci NULL,
  `immediate` bool NOT NULL DEFAULT false,
  `running` bool NOT NULL DEFAULT false,
  `runningtype` int(11) DEFAULT NULL,
  `created` varchar(14) COLLATE utf8_unicode_ci NULL,
  PRIMARY KEY (`eventID`),
  KEY `pbs_poolID` (`pbs_poolID`),
  KEY `pbs_membershipID` (`pbs_membershipID`),
  KEY `pbs_bootosID` (`pbs_bootosID`),
  KEY `pbs_bootmenuID` (`pbs_bootmenuID`),
  KEY `pbs_filterID` (`pbs_filterID`),
  KEY `category` (`category`),
  KEY `runningtype` (`runningtype`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `poolctrl_eventtype` (
  `eventtypeID` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(30) COLLATE utf8_unicode_ci NOT NULL UNIQUE,
  PRIMARY KEY (`eventtypeID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `poolctrl_eventcategory` (
  `eventcategoryID` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(30) COLLATE utf8_unicode_ci NOT NULL UNIQUE,
  PRIMARY KEY (`eventcategoryID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `poolctrl_eventreport` (
  `reportID` int(11) NOT NULL AUTO_INCREMENT,
  `result` varchar(140) COLLATE utf8_unicode_ci DEFAULT 'succeeded',
  `errors` varchar(140) COLLATE utf8_unicode_ci,
  `type` int(11) NOT NULL,
  `eventID` int(11) NOT NULL,
  `created` varchar(14) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`reportID`),
  KEY `eventID` (`eventID`),
  KEY `type` (`type`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

-- PBS2 Tabellen
-- TO DO: Tabellen an dieser Stelle nicht per Hand erzeugen sondern von der PBS2-Datenbank kopieren, sodass diese auf dem neusten Stand sind
CREATE TABLE IF NOT EXISTS `pbs_bootos` (
  `bootosID` int(11) NOT NULL AUTO_INCREMENT,
  `groupID` int(11) NOT NULL,
  `membershipID` int(11),
  `title` varchar(30) NOT NULL,
  `description` varchar(140),
  `distro` varchar(30),
  `distroversion` varchar(30),
  `source` varchar(140),
  `share` varchar(30),
  `shortname` varchar(30),
  `defaultkcl` varchar(255),
  `created` VARCHAR(14) NOT NULL,
  `expires` VARCHAR(14),
  `public` int(11) NOT NULL DEFAULT '-1',
  PRIMARY KEY (`bootosID`),
  KEY `groupID` (`groupID`),
  KEY `membershipID` (`membershipID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `pbs_config` (
  `configID` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(30) NOT NULL,
  `description` varchar(140),
  `groupID` int(11),
  `membershipID` int(11),
  `visible` tinyint(1) DEFAULT '0',
  `created` VARCHAR(14) NOT NULL,
  `bootosID` int(11) NOT NULL,
  PRIMARY KEY (`configID`),
  KEY `bootosID` (`bootosID`),
  KEY `groupID` (`groupID`),
  KEY `membershipID` (`membershipID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `pbs_pool` (
  `poolID` int(11) NOT NULL AUTO_INCREMENT,
  `groupID` int(11) NOT NULL,
  `title` varchar(30) NOT NULL,
  `description` varchar(140),
  `location` varchar(30),
  PRIMARY KEY (`poolID`),
  KEY `groupID` (`groupID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `pbs_client` (
  `clientID` int(11) NOT NULL AUTO_INCREMENT,
  `groupID` int(11) NOT NULL,
  `macadress` varchar(17) NOT NULL,
  `hardwarehash` varchar(32),
  `ip` varchar(15) NOT NULL,
  `ip6` varchar(45),
  `created` int(15),
  PRIMARY KEY (`clientID`),
  KEY `groupID` (`groupID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `pbs_poolentries` (
  `poolentriesID` INT NOT NULL AUTO_INCREMENT,
  `poolID` int(11) NOT NULL,
  `clientID` int(11) NOT NULL,
  PRIMARY KEY (`poolentriesID`),
  KEY `poolID` (`poolID`),
  KEY `clientID` (`clientID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;

-- Constraints
ALTER TABLE `pbs_config`
  ADD CONSTRAINT `pbs_config_ibfk` FOREIGN KEY (`bootosID`) REFERENCES `pbs_bootos` (`bootosID`) ON DELETE CASCADE; 

ALTER TABLE `pbs_poolentries`
  ADD CONSTRAINT `pbs_poolentries_ibfk_1` FOREIGN KEY (`poolID`) REFERENCES `pbs_pool` (`poolID`) ON DELETE CASCADE,
  ADD CONSTRAINT `pbs_poolentries_ibfk_2` FOREIGN KEY (`clientID`) REFERENCES `pbs_client` (`clientID`) ON DELETE CASCADE;

ALTER TABLE `poolctrl_eventreport`
  ADD CONSTRAINT `pbs_eventreport_eventidC` FOREIGN KEY (`eventID`) REFERENCES `poolctrl_event` (`eventID`) ON DELETE CASCADE,
  ADD CONSTRAINT `pbs_eventreport_typeidC` FOREIGN KEY (`type`) REFERENCES `poolctrl_eventtype` (`eventtypeID`) ON DELETE CASCADE;

ALTER TABLE `poolctrl_event`
  ADD CONSTRAINT `poolctrl_event_categoryC` FOREIGN KEY (`category`) REFERENCES `poolctrl_eventcategory` (`eventcategoryID`) ON DELETE CASCADE,
  ADD CONSTRAINT `poolctrl_event_runningtypeC` FOREIGN KEY (`runningtype`) REFERENCES `poolctrl_eventtype` (`eventtypeID`) ON DELETE SET NULL,
  ADD CONSTRAINT `poolctrl_event_startC` CHECK (start > CURRENT_TIMESTAMP()),
  ADD CONSTRAINT `poolctrl_event_endC` CHECK (end > start);
