CREATE TABLE IF NOT EXISTS `funsecretos`.`categories` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `category_id` INT(11) UNSIGNED NULL COMMENT 'Parent category',
  `user_id` INT(11) UNSIGNED NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `summary` VARCHAR(100) NULL,
  `image` VARCHAR(255) NOT NULL,
  `description` TEXT NULL,
  `order` INT(11) NULL DEFAULT 0,
  `level` TINYINT(4) NOT NULL DEFAULT 0 COMMENT 'Level of category, the max values allowed is 2. 0-Main category, 1-Subcategory, 2-Sub subcategory.',
  `show_in_landing` TINYINT(4) NULL DEFAULT 0 COMMENT '0-No, 1-Yes',
  `status` TINYINT(4) NULL DEFAULT 0 COMMENT '0-Inactive, 1-Active',
  `created_at` TIMESTAMP NULL,
  `updated_at` TIMESTAMP NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_categories_categories1_idx` (`category_id` ASC),
  INDEX `fk_categories_users1_idx` (`user_id` ASC),
  CONSTRAINT `fk_categories_categories1`
    FOREIGN KEY (`category_id`)
    REFERENCES `funsecretos`.`categories` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_categories_users1`
    FOREIGN KEY (`user_id`)
    REFERENCES `funsecretos`.`users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS `funsecretos`.`posts` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `category_id` INT(11) UNSIGNED NOT NULL,
  `user_id` INT(11) UNSIGNED NOT NULL COMMENT 'Post author.',
  `name` VARCHAR(45) NOT NULL,
  `summary` VARCHAR(100) NULL,
  `description` TEXT NOT NULL,
  `order` INT(11) NULL DEFAULT 0 COMMENT 'Order for show in landing page.',
  `status` TINYINT(4) NULL DEFAULT 0 COMMENT '0-Inactive, 1-Active',
  `created_at` TIMESTAMP NULL,
  `updated_at` TIMESTAMP NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_posts_users1_idx` (`user_id` ASC),
  INDEX `fk_posts_categories1_idx` (`category_id` ASC),
  CONSTRAINT `fk_posts_users1`
    FOREIGN KEY (`user_id`)
    REFERENCES `funsecretos`.`users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_posts_categories1`
    FOREIGN KEY (`category_id`)
    REFERENCES `funsecretos`.`categories` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS `funsecretos`.`post_files` (
  `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `post_id` INT(11) UNSIGNED NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `file` VARCHAR(255) NULL,
  `order` INT(11) NULL DEFAULT 0,
  `type` TINYINT(4) NOT NULL DEFAULT 0 COMMENT '0-Images, 1-Files.',
  `created_at` TIMESTAMP NULL,
  `updated_at` TIMESTAMP NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_post_files_posts1_idx` (`post_id` ASC),
  CONSTRAINT `fk_post_files_posts1`
    FOREIGN KEY (`post_id`)
    REFERENCES `funsecretos`.`posts` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;