-- -----------------------------------------------------
-- Schema db
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema db
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `MatematikaModerne` DEFAULT CHARACTER SET utf8 ;
USE `MatematikaModerne` ;


-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(45) NOT NULL,
  `role` ENUM('učitel', 'správce') NOT NULL,
  `email` VARCHAR(45) NOT NULL,
  `password` VARCHAR(45) NOT NULL,
  `school_type` ENUM('učiliště', 'střední škola', 'gymnázium') NOT NULL,
  `profile_image` VARCHAR(45) NULL DEFAULT NULL,
  `bio` TEXT NULL DEFAULT NULL,
  `last_change` TIMESTAMP NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
  UNIQUE INDEX `email_UNIQUE` (`email` ASC) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`topics`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `topics` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `articles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `articles` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(45) NOT NULL,
  `type` ENUM('fyzická', 'digitální') NOT NULL,
  `short_description` TEXT NULL,
  `content` LONGTEXT NOT NULL,
  `time_estimate` INT NOT NULL,
  `difficulty` ENUM('lehké', 'střední', 'těžké') NOT NULL,
  `usage_count` INT NULL,
  `created_at` TIMESTAMP NULL,
  `last_change` TIMESTAMP NULL,
  `users_id` INT NOT NULL,
  `topics_id` INT NOT NULL,
  `status` ENUM('APPROVED', 'REJECTED', 'IN_PROGRESS', 'TO_CHECK') NOT NULL DEFAULT 'IN_PROGRESS'
  PRIMARY KEY (`id`),
  INDEX `fk_articles_users_idx` (`users_id` ASC) ,
  INDEX `fk_articles_topics1_idx` (`topics_id` ASC) ,
  CONSTRAINT `fk_articles_users`
    FOREIGN KEY (`users_id`)
    REFERENCES `users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_articles_topics1`
    FOREIGN KEY (`topics_id`)
    REFERENCES `topics` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `comments`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `comments` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `text` TEXT NOT NULL,
  `created_at` TIMESTAMP NULL,
  `articles_id` INT NOT NULL,
  `parent_comments_id` INT NULL DEFAULT NULL,
  `users_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_comments_articles1_idx` (`articles_id` ASC) ,
  INDEX `fk_comments_comments1_idx` (`parent_comments_id` ASC) ,
  INDEX `fk_comments_users1_idx` (`users_id` ASC) ,
  CONSTRAINT `fk_comments_articles1`
    FOREIGN KEY (`articles_id`)
    REFERENCES `articles` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_comments_comments1`
    FOREIGN KEY (`parent_comments_id`)
    REFERENCES `comments` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_comments_users1`
    FOREIGN KEY (`users_id`)
    REFERENCES `users` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;