DROP TABLE IF EXISTS `documents`;
CREATE TABLE `documents` (
  `id` char(36) NOT NULL,
  `owner` varchar(45) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  `created` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `status` enum('nodata','inprogress','deleted','ok') NOT NULL DEFAULT 'nodata',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `documents_page`;
CREATE TABLE `documents_page` (
  `documentId` char(36) NOT NULL,
  `pageId` char(36) NOT NULL,
  PRIMARY KEY (`documentId`,`pageId`),
  KEY `fk_docpage_pageid_idx` (`pageId`),
  CONSTRAINT `fk_docpage_docid` FOREIGN KEY (`documentId`) REFERENCES `documents` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `fk_docpage_pageid` FOREIGN KEY (`pageId`) REFERENCES `pages` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `documents_tags`;
CREATE TABLE `documents_tags` (
  `documentId` char(36) NOT NULL,
  `tagId` char(36) NOT NULL,
  PRIMARY KEY (`documentId`,`tagId`),
  KEY `fk_tags_id_idx` (`tagId`),
  CONSTRAINT `fk_doctags_docid` FOREIGN KEY (`documentId`) REFERENCES `documents` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `fk_doctags_tagid` FOREIGN KEY (`tagId`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `lang`;
CREATE TABLE `lang` (
  `id` char(36) NOT NULL,
  `short` char(2) DEFAULT NULL,
  `name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `short_UNIQUE` (`short`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `pages`;
CREATE TABLE `pages` (
  `id` char(36) NOT NULL,
  `owner` varchar(45) DEFAULT NULL,
  `documentId` int(11) unsigned NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `status` enum('nodata','inprogress','deleted','ok') DEFAULT 'nodata',
  `contenttype` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `pages_words`;
CREATE TABLE `pages_words` (
  `pageId` char(36) NOT NULL,
  `wordId` char(36) NOT NULL DEFAULT 'en',
  PRIMARY KEY (`pageId`,`wordId`),
  KEY `fk_pagword_wordid_idx` (`wordId`),
  CONSTRAINT `fk_pagword_pageid` FOREIGN KEY (`pageId`) REFERENCES `pages` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `fk_pagword_wordid` FOREIGN KEY (`wordId`) REFERENCES `words` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `tags`;
CREATE TABLE `tags` (
  `id` char(36) NOT NULL,
  `tag` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tag_UNIQUE` (`tag`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
  `username` varchar(255) NOT NULL,
  `passwd` varchar(255) DEFAULT NULL,
  `groups` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `words`;
CREATE TABLE `words` (
  `id` char(36) NOT NULL,
  `word` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `word_UNIQUE` (`word`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `words_lang`;
CREATE TABLE `words_lang` (
  `wordId` char(36) NOT NULL,
  `langId` char(36) NOT NULL,
  PRIMARY KEY (`wordId`,`langId`),
  KEY `fk_worlan_langid_idx` (`langId`),
  CONSTRAINT `fk_worlan_langid` FOREIGN KEY (`langId`) REFERENCES `lang` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `fk_worlan_wordid` FOREIGN KEY (`wordId`) REFERENCES `words` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
