USE silas_beta_wordRotator;

DROP TABLE IF EXISTS Cronjob;
DROP TABLE IF EXISTS ChangeEmailCode;
DROP TABLE IF EXISTS RequestNewPasswordCode;
DROP TABLE IF EXISTS RegistrationCode;
DROP TABLE IF EXISTS UserCode;
DROP TABLE IF EXISTS RoleUser;
DROP TABLE IF EXISTS RoleAccess;
DROP TABLE IF EXISTS RoleChildren;
DROP TABLE IF EXISTS Role;
DROP TABLE IF EXISTS UserSetting;
DROP TABLE IF EXISTS UserAccess;
DROP TABLE IF EXISTS User;
DROP TABLE IF EXISTS Rating;
DROP TABLE IF EXISTS Level;
DROP TABLE IF EXISTS AuthToken;
DROP TABLE IF EXISTS Word;
DROP TABLE IF EXISTS Code;
DROP TABLE IF EXISTS Access;

CREATE TABLE Cronjob (id INT AUTO_INCREMENT NOT NULL, intervalInMinutes INT NOT NULL, lastRun DATETIME NOT NULL, className LONGTEXT NOT NULL, lastSuccess DATETIME NOT NULL, errorMessage LONGTEXT NOT NULL, active TINYINT(1) NOT NULL, version INT DEFAULT 1 NOT NULL, discr VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Code (id INT AUTO_INCREMENT NOT NULL, code VARCHAR(255) NOT NULL, creationDate DATETIME NOT NULL, isCacheable TINYINT(1) NOT NULL, version INT DEFAULT 1 NOT NULL, discriminator VARCHAR(255) NOT NULL, UNIQUE INDEX UNIQ_817996E977153098 (code), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE UserCode (id INT NOT NULL, userId INT DEFAULT NULL, INDEX IDX_E2BA727E64B64DCC (userId), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE ChangeEmailCode (id INT NOT NULL, newEmail VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Access (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(75) NOT NULL, beschreibung VARCHAR(255) NOT NULL, version INT DEFAULT 1 NOT NULL, UNIQUE INDEX UNIQ_FF893525E237E06 (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE RegistrationCode (id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Role (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(75) NOT NULL, beschreibung VARCHAR(255) NOT NULL, version INT DEFAULT 1 NOT NULL, UNIQUE INDEX UNIQ_A1052C1B5E237E06 (name), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE RoleUser (RoleId INT NOT NULL, UserId INT NOT NULL, INDEX IDX_2DDE86A9BF6EF8BE (RoleId), INDEX IDX_2DDE86A9631A48FA (UserId), PRIMARY KEY(RoleId, UserId)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE RoleAccess (RoleId INT NOT NULL, AccessId INT NOT NULL, INDEX IDX_337C2901BF6EF8BE (RoleId), INDEX IDX_337C2901A1A046DD (AccessId), PRIMARY KEY(RoleId, AccessId)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE RoleChildren (ParentId INT NOT NULL, ChildId INT NOT NULL, INDEX IDX_88554C92E9982EB8 (ParentId), INDEX IDX_88554C92CD4052DB (ChildId), PRIMARY KEY(ParentId, ChildId)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE UserSetting (id INT AUTO_INCREMENT NOT NULL, userId INT NOT NULL, name VARCHAR(255) NOT NULL, value LONGTEXT NOT NULL, lastUpdated BIGINT NOT NULL, version INT DEFAULT 1 NOT NULL, INDEX IDX_B63D87E164B64DCC (userId), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE User (id INT AUTO_INCREMENT NOT NULL, username VARCHAR(255) NOT NULL, password VARCHAR(128) NOT NULL, email VARCHAR(255) NOT NULL, activated TINYINT(1) NOT NULL, blocked TINYINT(1) NOT NULL, version INT DEFAULT 1 NOT NULL, UNIQUE INDEX UNIQ_7BFF7038F85E0677 (username), UNIQUE INDEX UNIQ_7BFF7038E7927C74 (email), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE UserAccess (UserId INT NOT NULL, AccessId INT NOT NULL, INDEX IDX_26623A76631A48FA (UserId), INDEX IDX_26623A76A1A046DD (AccessId), PRIMARY KEY(UserId, AccessId)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE RequestNewPasswordCode (id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Level (id INT AUTO_INCREMENT NOT NULL, words VARCHAR(255) NOT NULL, positions VARCHAR(255) NOT NULL, renderer INT NOT NULL, lastUpdated DATETIME NOT NULL, lang INT NOT NULL, deleted TINYINT(1) NOT NULL, difficulty INT NOT NULL, version INT DEFAULT 1 NOT NULL, UNIQUE INDEX levelCore (words, positions, renderer, lang), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Word (id INT AUTO_INCREMENT NOT NULL, word LONGTEXT NOT NULL, created DATETIME NOT NULL, lastUpdated DATETIME NOT NULL, deleted TINYINT(1) NOT NULL, lang INT NOT NULL, checked INT NOT NULL, version INT DEFAULT 1 NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE Rating (id INT AUTO_INCREMENT NOT NULL, rating INT NOT NULL, version INT DEFAULT 1 NOT NULL, levelId INT DEFAULT NULL, authTokenId INT DEFAULT NULL, INDEX IDX_D1189E24D9801611 (levelId), INDEX IDX_D1189E24A1F3197E (authTokenId), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE AuthToken (id INT AUTO_INCREMENT NOT NULL, token LONGTEXT NOT NULL, name LONGTEXT NOT NULL, creationDate DATETIME DEFAULT NULL, version INT DEFAULT 1 NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE UserCode ADD CONSTRAINT FK_E2BA727E64B64DCC FOREIGN KEY (userId) REFERENCES User (id);
ALTER TABLE UserCode ADD CONSTRAINT FK_E2BA727EBF396750 FOREIGN KEY (id) REFERENCES Code (id) ON DELETE CASCADE;
ALTER TABLE ChangeEmailCode ADD CONSTRAINT FK_8FAE5EFBF396750 FOREIGN KEY (id) REFERENCES Code (id) ON DELETE CASCADE;
ALTER TABLE RegistrationCode ADD CONSTRAINT FK_96DE2014BF396750 FOREIGN KEY (id) REFERENCES Code (id) ON DELETE CASCADE;
ALTER TABLE RoleUser ADD CONSTRAINT FK_2DDE86A9BF6EF8BE FOREIGN KEY (RoleId) REFERENCES Role (id);
ALTER TABLE RoleUser ADD CONSTRAINT FK_2DDE86A9631A48FA FOREIGN KEY (UserId) REFERENCES User (id);
ALTER TABLE RoleAccess ADD CONSTRAINT FK_337C2901BF6EF8BE FOREIGN KEY (RoleId) REFERENCES Role (id);
ALTER TABLE RoleAccess ADD CONSTRAINT FK_337C2901A1A046DD FOREIGN KEY (AccessId) REFERENCES Access (id);
ALTER TABLE RoleChildren ADD CONSTRAINT FK_88554C92E9982EB8 FOREIGN KEY (ParentId) REFERENCES Role (id);
ALTER TABLE RoleChildren ADD CONSTRAINT FK_88554C92CD4052DB FOREIGN KEY (ChildId) REFERENCES Role (id);
ALTER TABLE UserSetting ADD CONSTRAINT FK_B63D87E164B64DCC FOREIGN KEY (userId) REFERENCES User (id);
ALTER TABLE UserAccess ADD CONSTRAINT FK_26623A76631A48FA FOREIGN KEY (UserId) REFERENCES User (id);
ALTER TABLE UserAccess ADD CONSTRAINT FK_26623A76A1A046DD FOREIGN KEY (AccessId) REFERENCES Access (id);
ALTER TABLE RequestNewPasswordCode ADD CONSTRAINT FK_27CF9C8BBF396750 FOREIGN KEY (id) REFERENCES Code (id) ON DELETE CASCADE;
ALTER TABLE Rating ADD CONSTRAINT FK_D1189E24D9801611 FOREIGN KEY (levelId) REFERENCES Level (id);
ALTER TABLE Rating ADD CONSTRAINT FK_D1189E24A1F3197E FOREIGN KEY (authTokenId) REFERENCES AuthToken (id);
ALTER TABLE Word ADD used TINYINT(1) NOT NULL;


INSERT INTO Role (name, beschreibung) VALUES ('gast', 'Kein aktives oder eingeloggtes Mitglied'); -- 1
INSERT INTO Role (name, beschreibung) VALUES ('user', 'Normales Mitglied'); -- 2
INSERT INTO Role (name, beschreibung) VALUES ('admin', 'Admin, darf alles'); -- 3

INSERT INTO RoleChildren (ParentId, ChildId) VALUES (2, 3);

INSERT INTO Access (name, beschreibung) VALUES ('offline', 'alles, was ein nicht eingeloggter User sehen darf'); -- 1
INSERT INTO Access (name, beschreibung) VALUES ('online', 'alles, was ein eingeloggter User sehen darf'); -- 2
INSERT INTO Access (name, beschreibung) VALUES ('default', 'darf jeder'); -- 3
INSERT INTO `Access` (`name`, `beschreibung`) VALUES ('admin', 'darf nur ein Admin'); -- 4
INSERT INTO RoleAccess (RoleId, AccessId) VALUES (1,1); -- gast - offline
INSERT INTO RoleAccess(RoleId, AccessId) VALUES (1,3); -- gast - default
INSERT INTO RoleAccess(RoleId, AccessId) VALUES (2,2); -- user - online
INSERT INTO RoleAccess(RoleId, AccessId) VALUES (2,3); -- user - default
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (3, 4); -- admin - admin



INSERT INTO `Level` (`id`, `words`, `positions`, `renderer`, `lastUpdated`, `lang`, `deleted`, `difficulty`) VALUES
(15, '["ARCHIV","CHARME"]', '[1,1,3]', 20, '2018-07-14 17:01:18', 1, 0, 2),
(26, '["BRAUEREI","THERAPIE"]', '[3,2,0,1]', 40, '2018-07-14 17:01:18', 1, 0, 40),
(217, '["ZEITSTRAFE","HOCHSAISON"]', '[1,0,3,3,1]', 60, '2018-07-14 17:01:18', 1, 0, 60),
(220, '["FEINGUSS","R\\u00dcCKFALL","PHYSIKER","RESIDENZ","BERATUNG","KOCHTOPF"]', '[2,0,0,1,2,2,3,3,2,1,1,2,0,0]', 100, '2018-07-14 17:01:18', 1, 0, 100),
(24, '["BETONUNG","ANBETUNG","ALLERGIE","BAUMHAUS"]', '[0,3,1,1,3,1,2,0,0,0]', 120, '2018-07-14 17:01:18', 1, 0, 120),
(62, '["FEHLPROGNOSE","GEISTESKRAFT","ARBEITSPAUSE","BEREITSCHAFT","MOSAIKARBEIT","INFRAROTFILM"]', '[0,3,0,1,0,3,2,3,1,2,3,1,2,2,0,2,3,0,2,1,1]', 140, '2018-07-14 17:01:18', 1, 0, 140),
(260, '["SCHREIBTISCH","URHEBERRECHT","PFLANZENKOST","OPERNKONZERT"]', '[1,3,2,1,3,0,1,2,3,0,2,0,1,0,0]', 160, '2018-07-14 17:01:18', 1, 0, 160),
(67,'["AHNUNG","AUSBAU"]','[0,0,1]',20,'2018-10-10 00:00:00',1,0,1),
(341,'["NACHLASS","BUSLINIE","REITBAHN","LAUFGANG","KASCHMIR","BADETUCH"]','[1,3,0,2,3,0,1,0,0,0,3,1,0,0]',100,'2018-04-15 12:22:11',1,0,61)
;