-- smd__template_folder definition

CREATE TABLE `smd__template_folder` (
  `folderID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `organizationID` int(11) unsigned NOT NULL,
  `userID` int(11) unsigned NOT NULL,
  `type` enum('user','organization') NOT NULL DEFAULT 'user',
  `status` enum('active','archived') NOT NULL DEFAULT 'active',
  `editable` tinyint(1) NOT NULL DEFAULT '0',
  `assignTemplates` tinyint(1) NOT NULL DEFAULT '0',
  `smart` tinyint(1) NOT NULL DEFAULT '0',
  `pinned` tinyint(1) NOT NULL DEFAULT '0',
  `childOrgSharing` tinyint(1) NOT NULL DEFAULT '0',
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updatedAt` timestamp NULL DEFAULT NULL,
  `lastUpdateAction` enum('edit','share','archive') DEFAULT NULL,
  `lastUpdatedBy` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`folderID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

-- smd__template_folder_has_organizations definition

CREATE TABLE `smd__template_folder_has_organizations` (
  `folderID` int(11) unsigned NOT NULL,
  `organizationID` int(11) unsigned NOT NULL,
  PRIMARY KEY (`folderID`,`organizationID`),
  KEY `smd__template_folder_has_organizations_FK` (`organizationID`),
  CONSTRAINT `smd__template_folder_has_organizations_FK` FOREIGN KEY (`organizationID`) REFERENCES `smd__organization` (`organizationID`),
  CONSTRAINT `smd__template_folder_has_organizations_FK_1` FOREIGN KEY (`folderID`) REFERENCES `smd__template_folder` (`folderID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- smd__template_folder_has_tags definition

CREATE TABLE `smd__template_folder_has_tags` (
  `folderID` int(11) unsigned NOT NULL,
  `tagID` int(11) unsigned NOT NULL,
  PRIMARY KEY (`folderID`,`tagID`),
  KEY `smd__template_folder_has_tags_FK_1` (`tagID`),
  CONSTRAINT `smd__template_folder_has_tags_FK` FOREIGN KEY (`folderID`) REFERENCES `smd__template_folder` (`folderID`),
  CONSTRAINT `smd__template_folder_has_tags_FK_1` FOREIGN KEY (`tagID`) REFERENCES `cms__tag` (`tagID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- smd__template_folder_has_templates definition

CREATE TABLE `smd__template_folder_has_templates` (
  `folderID` int(11) unsigned NOT NULL,
  `templateID` int(11) unsigned NOT NULL,
  `addedAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`folderID`,`templateID`),
  KEY `smd__template_folder_has_templates_FK` (`templateID`),
  CONSTRAINT `smd__template_folder_has_templates_FK` FOREIGN KEY (`templateID`) REFERENCES `smd__post_template` (`templateID`),
  CONSTRAINT `smd__template_folder_has_templates_FK_1` FOREIGN KEY (`folderID`) REFERENCES `smd__template_folder` (`folderID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;