CREATE TABLE silas_wordRotator.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 silas_wordRotator.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 silas_wordRotator.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 silas_wordRotator.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 silas_wordRotator.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 silas_wordRotator.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 silas_wordRotator.RegistrationCode (id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
     CREATE TABLE silas_wordRotator.RequestNewPasswordCode (id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
     CREATE TABLE silas_wordRotator.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 silas_wordRotator.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;
     ALTER TABLE silas_wordRotator.UserCode ADD CONSTRAINT FK_E2BA727E64B64DCC FOREIGN KEY (userId) REFERENCES silas_wordRotator.User (id);
     ALTER TABLE silas_wordRotator.UserCode ADD CONSTRAINT FK_E2BA727EBF396750 FOREIGN KEY (id) REFERENCES silas_wordRotator.Code (id) ON DELETE CASCADE;
     ALTER TABLE silas_wordRotator.ChangeEmailCode ADD CONSTRAINT FK_8FAE5EFBF396750 FOREIGN KEY (id) REFERENCES silas_wordRotator.Code (id) ON DELETE CASCADE;
     ALTER TABLE UserAccess ADD CONSTRAINT FK_26623A76631A48FA FOREIGN KEY (UserId) REFERENCES silas_wordRotator.User (id);
     ALTER TABLE UserAccess ADD CONSTRAINT FK_26623A76A1A046DD FOREIGN KEY (AccessId) REFERENCES silas_wordRotator.Access (id);
     ALTER TABLE RoleUser ADD CONSTRAINT FK_2DDE86A9BF6EF8BE FOREIGN KEY (RoleId) REFERENCES silas_wordRotator.Role (id);
     ALTER TABLE RoleUser ADD CONSTRAINT FK_2DDE86A9631A48FA FOREIGN KEY (UserId) REFERENCES silas_wordRotator.User (id);
     ALTER TABLE RoleAccess ADD CONSTRAINT FK_337C2901BF6EF8BE FOREIGN KEY (RoleId) REFERENCES silas_wordRotator.Role (id);
     ALTER TABLE RoleAccess ADD CONSTRAINT FK_337C2901A1A046DD FOREIGN KEY (AccessId) REFERENCES silas_wordRotator.Access (id);
     ALTER TABLE RoleChildren ADD CONSTRAINT FK_88554C92E9982EB8 FOREIGN KEY (ParentId) REFERENCES silas_wordRotator.Role (id);
     ALTER TABLE RoleChildren ADD CONSTRAINT FK_88554C92CD4052DB FOREIGN KEY (ChildId) REFERENCES silas_wordRotator.Role (id);
     ALTER TABLE silas_wordRotator.RegistrationCode ADD CONSTRAINT FK_96DE2014BF396750 FOREIGN KEY (id) REFERENCES silas_wordRotator.Code (id) ON DELETE CASCADE;
     ALTER TABLE silas_wordRotator.RequestNewPasswordCode ADD CONSTRAINT FK_27CF9C8BBF396750 FOREIGN KEY (id) REFERENCES silas_wordRotator.Code (id) ON DELETE CASCADE;
     ALTER TABLE silas_wordRotator.UserSetting ADD CONSTRAINT FK_B63D87E164B64DCC FOREIGN KEY (userId) REFERENCES silas_wordRotator.User (id);

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

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

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

INSERT INTO Role (name, beschreibung) VALUES ('bookbinderAutor', 'Autor von Büchern in Bookbinder');
INSERT INTO `Access` (`id`, `name`, `beschreibung`) VALUES (NULL, 'bookbinderViewBooks', 'erlaubt das Ansehen der eigenen Bücher'), (NULL, 'bookbinderModifyBook', 'erlaubt das Erstellen und Verändern eigener Bücher'), (NULL, 'bookbinderDeleteBook', 'erlaubt das Löschen eigener Bücher'), (NULL, 'bookbinderBookoverwiev', 'erlaubt ein Buch anzusehen'), (NULL, 'bookbinderModifyChapter', 'erlaubt das Erstellen und Verändern eines Kapitels'), (NULL, 'bookbinderDeleteChapter', 'erlaubt das Löschen eines Kapitels'), (NULL, 'bookbinderChangeChapterPos', 'erlaubt das Verändern der Reihenfolge der Kapitel'), (NULL, 'bookbinderToPdf', 'erlaubt das Konvertieren zur PDF des Buches'), (NULL, 'bookbinderMakePublic', 'erlaubt das Veröffentlichen eines Buches');
INSERT INTO `RoleChildren` (`ParentId`, `ChildId`) VALUES ('4', '3');

INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 5);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 6);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 7);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 8);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 9);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 10);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 11);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 12);
INSERT INTO `RoleAccess` (`RoleId`, `AccessId`) VALUES (4, 13);


insert into `Access` set name='playStories', beschreibung='darf Stories spielen';
insert into `Role` set name='player', beschreibung='Story-player';
insert into `RoleAccess` set RoleId=5, AccessId=14;


-- ######################################################

insert into `Access` set name='editStory', beschreibung='darf stories bearbeiten';
insert into `Role` set name='storyAuthor', beschreibung='Story-Author';
insert into `RoleAccess` set RoleId=6, AccessId=15;


-- #######################Test-Data##################
insert into `Story` set name='firstStory', imgPath='img/test.png';
insert into `Chat` set storyId=1, imgPath='img/test.png', description='Familien-Chat', version=1, name='familie <3';
insert into `Author` set isUser=1, imgPath='img/test.png', description='It is a me, mario', name='Mario';
insert into `Author` set isUser=0, imgPath='img/test.png', description='Mein Bruder', name='Luigi';
insert into `Author` set isUser=0, imgPath='img/test.png', description='Meine Mutter', version=1, name='Mutter';
insert into `ChatAuthors` set authorId=1, chatId=1;
insert into `ChatAuthors` set authorId=2, chatId=1;
insert into `ChatAuthors` set authorId=3, chatId=1;
insert into `ConditionObject` set discriminator='sequence';
insert into `Sequence` set id=1, storyId=1;
update `Story` set `startSequenceId`='1' where `id`='1' limit 1;
insert into `Message` set sequenceId=1, atTime='-1', authorId=3, orderIndex=1, timeOffset=0, message='Kommt ihr zu ostern heim?', chatId=1;
insert into `Message` set sequenceId=1, atTime='-1', version=1, authorId=2, orderIndex=2, timeOffset=0, message='Nein', chatId=1;
insert into `Message` set sequenceId=1, atTime='-1', authorId=3, version=1, orderIndex=3, timeOffset=0, message='Schade, Anderer-Mario', id=3, chatId=1;
insert into `Message` set atTime='-1', authorId=1, version=1, orderIndex=4, timeOffset=0, message='Ja, Mama', chatId=1;
insert into `Message` set atTime='-1', authorId=1, version=1, orderIndex=4, timeOffset=0, message='Nein, ich kann nicht.', chatId=1;
insert into `ConditionObject` set discriminator='answer';
insert into `ConditionObject` set discriminator='answer';
insert into `Answer` set sequenceId=1, id=2, messageId=4;
insert into `Answer` set sequenceId=1, id=3, messageId=5;

insert into `ConditionObject` set discriminator='sequence';
insert into `ConditionObject` set discriminator='sequence';
insert into `Sequence` set id=4, storyId=1;
insert into `Sequence` set id=5, storyId=1;
insert into `Message` set sequenceId=4, atTime='-1', authorId=3, version=1, orderIndex=5, timeOffset=0, message='Ok...', chatId=1;
insert into `Message` set sequenceId=5, atTime='-1', authorId=3, version=1, orderIndex=5, timeOffset=0, message='Juhu!', id=8, chatId=1;
insert into `NextSequences` set SequenceId=5, AnswerId=2;
insert into `NextSequences` set SequenceId=4, AnswerId=3;
