SET default_storage_engine = InnoDB;
DROP DATABASE IF EXISTS `nuitdelinfo2023`;
CREATE DATABASE IF NOT EXISTS `nuitdelinfo2023`
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;

DROP USER IF EXISTS 'nuitdelinfo2023';
CREATE USER 'nuitdelinfo2023'@'localhost' IDENTIFIED BY 'PASSWORD';
GRANT ALL PRIVILEGES ON nuitdelinfo2023.* TO 'nuitdelinfo2023'@'localhost';

USE `nuitdelinfo2023`;

CREATE TABLE users (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  username VARCHAR(64) NOT NULL,
  password VARCHAR(255) NOT NULL,
  score INT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (id),
  INDEX ur_username_idx (username)
) ENGINE=InnoDB;

CREATE TABLE games (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  player INT UNSIGNED NOT NULL,
  PRIMARY KEY (id),
  INDEX g_player_idx (player)
) ENGINE=InnoDB;

CREATE TABLE themes (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  theme VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB;

CREATE TABLE questions (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  theme INT UNSIGNED NOT NULL,
  question VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  INDEX q_theme_idx (theme)
) ENGINE=InnoDB;

CREATE TABLE answers (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  question INT UNSIGNED NOT NULL,
  answer VARCHAR(255) NOT NULL,
  correct BOOLEAN NOT NULL DEFAULT FALSE,
  PRIMARY KEY (id),
  INDEX a_question_idx (question)
) ENGINE=InnoDB;

CREATE TABLE game_questions (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  game INT UNSIGNED NOT NULL,
  question INT UNSIGNED NOT NULL,
  score INT UNSIGNED,
  PRIMARY KEY (id),
  INDEX gq_game_idx (game),
  INDEX gq_question_idx (question)
) ENGINE=InnoDB;


INSERT INTO themes (theme) VALUES ('Environnement');
INSERT INTO themes (theme) VALUES ('Energie Renouvelable');
INSERT INTO themes (theme) VALUES ("Recyclage et consommation d'energie");
INSERT INTO themes (theme) VALUES ('Pollution et energie fossile');
INSERT INTO themes (theme) VALUES ('Politique');
