CREATE TABLE IF NOT EXISTS `#__docman_documents` (
  `docman_document_id` BIGINT(20)  UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
  `uuid` char(36) NOT NULL UNIQUE,
  `title` varchar(255) NOT NULL,
  `slug` varchar(255) NOT NULL UNIQUE,
  `docman_category_id` bigint(20) UNSIGNED NOT NULL,
  `description` longtext,
  `image` varchar(512) NOT NULL default '',
  `storage_type` varchar(64) NOT NULL default '',
  `storage_path` varchar(512) NOT NULL default '',
  `hits` int(11) NOT NULL default 0,
  `enabled` tinyint(1) NOT NULL default 1,
  `publish_on` datetime DEFAULT NULL,
  `unpublish_on` datetime DEFAULT NULL,
  `locked_on` datetime DEFAULT NULL,
  `locked_by` bigint(20) NOT NULL default 0,
  `created_on` datetime DEFAULT NULL,
  `created_by` bigint(20) NOT NULL default 0,
  `modified_on` datetime DEFAULT NULL,
  `modified_by` bigint(20) NOT NULL default 0,
  `params` text,
  `asset_id` INTEGER UNSIGNED NOT NULL DEFAULT 0,
	`ordering` int(11) NOT NULL default 0,
  KEY `category_index` (`docman_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_usergroups` (
    `docman_usergroup_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(255) NOT NULL DEFAULT '',
    `description` text,
    `created_by` bigint(20) unsigned DEFAULT NULL,
    `created_on` datetime NOT NULL,
    `modified_by` bigint(20) unsigned NOT NULL,
    `modified_on` datetime NOT NULL,
    `internal` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`docman_usergroup_id`),
  UNIQUE KEY `name` (`name`,`internal`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_usergroups_users` (
  `docman_usergroup_id` bigint(20) unsigned NOT NULL,
  `wp_user_id` bigint(20) unsigned NOT NULL,
  PRIMARY KEY (`docman_usergroup_id`,`wp_user_id`),
  KEY `wp_user_id` (`wp_user_id`),
  CONSTRAINT `#__docman_usergroups_users_ibfk_1` FOREIGN KEY (`docman_usergroup_id`) REFERENCES `#__docman_usergroups` (`docman_usergroup_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_categories` (
    `docman_category_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
  	`uuid` char(36) NOT NULL UNIQUE,
    `title` varchar(255) NOT NULL,
    `slug` varchar(255) NOT NULL UNIQUE,
    `description` text,
    `image` varchar(512) NOT NULL default '',
    `params` text,
    `inherit_category_group_access` bigint(20) unsigned DEFAULT NULL,
    `inherit_document_group_access` bigint(20) unsigned DEFAULT NULL,
    `inherit_permissions` bigint(20) unsigned DEFAULT NULL,
    `enabled` tinyint(1) NOT NULL default 1,
    `locked_on` datetime DEFAULT NULL,
    `locked_by` bigint(20) NOT NULL default 0,
    `created_on` datetime DEFAULT NULL,
    `created_by` bigint(20) NOT NULL default 0,
    `modified_on` datetime DEFAULT NULL,
    `modified_by` bigint(20) NOT NULL default 0,
    `asset_id` INTEGER UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_categories_permissions` (
  `docman_category_id` bigint(20) unsigned NOT NULL,
  `action` tinyint(3) unsigned NOT NULL,
  `wp_user_id` bigint(32) unsigned NOT NULL,
	`allowed` tinyint(1) NOT NULL,
  UNIQUE KEY `category_action_user` (`docman_category_id`,`action`,`wp_user_id`),
  KEY `action_user` (`action`, `wp_user_id`),
  KEY `user` (`wp_user_id`),
  KEY `action` (`action`),
  KEY `category` (`docman_category_id`),
	KEY `allowed` (`allowed`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_users` (
  `wp_user_id` bigint(32) NOT NULL,
  `permissions_map` text,
  `roles_hash` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`wp_user_id`),
  KEY `#__docman_users_wp_user_id_roles_hash_idx` (`roles_hash`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_permissions` (
  `docman_permission_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `table` varchar(255) NOT NULL DEFAULT '',
  `row` bigint(20) unsigned NOT NULL,
  `data` text,
  PRIMARY KEY (`docman_permission_id`),
  UNIQUE KEY `table` (`table`,`row`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_category_group_access` (
    `docman_category_id` bigint(20) unsigned NOT NULL,
    `docman_usergroup_id` bigint(20) NOT NULL,
    PRIMARY KEY (`docman_category_id`,`docman_usergroup_id`),
    KEY `docman_usergroup_id` (`docman_usergroup_id`),
    CONSTRAINT `#__docman_category_group_access_ibfk_1` FOREIGN KEY (`docman_category_id`) REFERENCES `#__docman_categories` (`docman_category_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_document_group_access` (
    `docman_category_id` bigint(20) unsigned NOT NULL,
    `docman_usergroup_id` bigint(20) NOT NULL,
    PRIMARY KEY (`docman_category_id`,`docman_usergroup_id`),
    KEY `docman_usergroup_id` (`docman_usergroup_id`),
    CONSTRAINT `#__docman_document_group_access_ibfk_1` FOREIGN KEY (`docman_category_id`) REFERENCES `#__docman_categories` (`docman_category_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_category_relations` (
  `ancestor_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `descendant_id` bigint(20) unsigned NOT NULL DEFAULT '0',
  `level` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ancestor_id`, `descendant_id`, `level`),
  KEY `path_index` (`descendant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_category_orderings` (
  `docman_category_id` bigint(20) unsigned NOT NULL,
  `title` int(11) NOT NULL DEFAULT '0',
  `custom` int(11) NOT NULL DEFAULT '0',
  `created_on` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`docman_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_category_folders` (
  `docman_category_id` bigint(20) unsigned NOT NULL,
  `folder` varchar(4096) NOT NULL DEFAULT '',
  `automatic` tinyint(1) NOT NULL DEFAULT '0',
  UNIQUE KEY `docman_category_id` (`docman_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_files` (
  `docman_file_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `folder` varchar(2048) NOT NULL DEFAULT '',
  `name` varchar(2048) NOT NULL DEFAULT '',
  `modified_on` datetime DEFAULT NULL,
  `modified_by` bigint(20) NOT NULL,
  `created_on` datetime DEFAULT NULL,
  `created_by` bigint(20) NOT NULL,
  `parameters` text,
  PRIMARY KEY (`docman_file_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_folders` (
  `docman_folder_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `folder` varchar(2048) NOT NULL DEFAULT '',
  `name` varchar(2048) NOT NULL DEFAULT '',
  `modified_on` datetime DEFAULT NULL,
  `modified_by` bigint(20) NOT NULL,
  `created_on` datetime DEFAULT NULL,
  `created_by` bigint(20) NOT NULL,
  `parameters` text,
  PRIMARY KEY (`docman_folder_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_tags` (
  `tag_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `slug` varchar(255) NOT NULL,
	`count` int(11) DEFAULT '0',
  `created_by` int(10) UNSIGNED DEFAULT NULL,
  `created_on` datetime DEFAULT NULL,
  `modified_by` int(10) UNSIGNED DEFAULT NULL,
  `modified_on` datetime DEFAULT NULL,
  `locked_by` int(10) UNSIGNED DEFAULT NULL,
  `locked_on` datetime DEFAULT NULL,
  `params` text NOT NULL,
  PRIMARY KEY (`tag_id`),
  UNIQUE KEY `slug` (`slug`),
  UNIQUE KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_tags_relations` (
  `tag_id` bigint(20) UNSIGNED NOT NULL,
  `row` varchar(36) NOT NULL,
  PRIMARY KEY (`tag_id`, `row`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_scans` (
  `docman_scan_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
  `identifier` varchar(64) NOT NULL DEFAULT '',
  `status` tinyint(1) NOT NULL DEFAULT '0',
  `ocr` tinyint(1) NOT NULL DEFAULT '0',
  `thumbnail` tinyint(1) NOT NULL DEFAULT '0',
  `modified_on` datetime DEFAULT NULL,
  `created_on` datetime DEFAULT NULL,
  `response` varchar(2048) NOT NULL DEFAULT '',
  `sent_on` datetime DEFAULT NULL,
  `retries` tinyint(1) unsigned DEFAULT 0,
  `parameters` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_routes` (
  `uuid` char(36) NOT NULL,
  `type` varchar(64) NOT NULL DEFAULT '',
  `path` varchar(2048) NOT NULL DEFAULT '',
  UNIQUE KEY `uuid` (`uuid`),
  KEY `path_idx` (`path`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_document_contents` (
  `docman_document_id` bigint(20) unsigned NOT NULL,
  `contents` longtext,
  UNIQUE KEY `docman_document_id` (`docman_document_id`),
  FOREIGN KEY (`docman_document_id`) REFERENCES `#__docman_documents`(`docman_document_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__docman_notifications` (
  `docman_notification_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `row` bigint(20) unsigned NOT NULL,
  `table` varchar(255) NOT NULL,
  `description` text,
  `notifier` varchar(255) NOT NULL,
  `created_by` bigint(20) unsigned NOT NULL,
  `created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified_by` bigint(20) unsigned NOT NULL,
  `modified_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `inheritable` tinyint(1) DEFAULT '0',
  `parameters` text NOT NULL,
  PRIMARY KEY (`docman_notification_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS  `#__docman_emails` (
  `docman_email_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `docman_notification_id` bigint(20) unsigned NOT NULL,
  `recipient` varchar(320) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `subject` text,
  `body` text,
  `status` tinyint(1) DEFAULT '0',
  `created_on` datetime DEFAULT NULL,
  `sent_on` datetime DEFAULT NULL,
  `retries` tinyint DEFAULT '0',
  `parameters` text CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci,
  PRIMARY KEY (`docman_email_id`),
  KEY `docman_emails_recipient_idx` (`recipient`) USING BTREE,
  KEY `docman_notification_id` (`docman_notification_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;