# Script de creation de la structure de la base OLA (a executer en root)
-- INSTALLER LA DERNIERE VERSION DE MARIADB
-- LANCER CE SCRIPT AVEC LA COMMANDE : mysql -u root < OLA.mysql

# Create schemas and users
CREATE DATABASE IF NOT EXISTS OLA CHARACTER SET utf8 COLLATE utf8_general_ci;


CREATE USER IF NOT EXISTS 'ola'@'localhost';
SET PASSWORD FOR 'ola'@'localhost' = PASSWORD('ola');
GRANT SELECT, INSERT, UPDATE, DELETE ON OLA.* TO 'ola'@'localhost';

USE OLA;

# Create tables
CREATE TABLE IF NOT EXISTS SETTINGS
(
    `key` VARCHAR(128) NOT NULL,
    `value` VARCHAR(256) NOT NULL,
    description VARCHAR(256),
    PRIMARY KEY(`key`)
) ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS `GROUP`
(
  id             BIGINT NOT NULL AUTO_INCREMENT,
  `name`         VARCHAR(128) NOT NULL UNIQUE,
  `year`         CHARACTER(4) NOT NULL,
  class_short    VARCHAR(128) NOT NULL,
  class_long     VARCHAR(512),
  department     VARCHAR(256),
  resp_id        BIGINT,
  sec_id         BIGINT,
  ressources_dir VARCHAR(512),
    PRIMARY KEY(id)
) ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS `USER`
(
  id     BIGINT       NOT NULL AUTO_INCREMENT,
  `role` VARCHAR(10)  NOT NULL,
  email  VARCHAR(128) NOT NULL,
  name   VARCHAR(128) NOT NULL,
  psw    VARCHAR(256)          DEFAULT NULL,
  hash   VARCHAR(128),
  phone  VARCHAR(15),
    PRIMARY KEY(id)
) ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS TUTORSHIP
(
    id BIGINT NOT NULL AUTO_INCREMENT,
    group_id BIGINT NOT NULL,
    student_id BIGINT NOT NULL,
    ptutor_id BIGINT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS LIVRET
(
  id              BIGINT       NOT NULL AUTO_INCREMENT,
  tutorship_id    BIGINT       NOT NULL,
  etutor_id       BIGINT       NOT NULL,
  company_name    VARCHAR(256) NOT NULL,
  company_address VARCHAR(512) NOT NULL,
  contract_type   INT          NOT NULL,
  contract_start  DATE         NOT NULL,
  contract_end    DATE         NOT NULL,
  ressources_dir  VARCHAR(512),
  opened          TINYINT(1)   NOT NULL,
  expire          DATE         NOT NULL,
    PRIMARY KEY(id)
) ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS PERIOD
(
    id             BIGINT NOT NULL AUTO_INCREMENT,
    livret_id      BIGINT NOT NULL,
    `type`         INT,
    `start`        DATE,
    `end`          DATE,
    student_desc   TEXT,
    etutor_desc    TEXT,
    ressources_dir VARCHAR(512),
    PRIMARY KEY (id)
) ENGINE = INNODB;


# Create FKs
ALTER TABLE `GROUP`
    ADD    FOREIGN KEY (resp_id)
    REFERENCES `USER`(id);

ALTER TABLE `GROUP`
  ADD FOREIGN KEY (sec_id)
REFERENCES `USER` (id)
;

ALTER TABLE TUTORSHIP
    ADD    FOREIGN KEY (group_id)
    REFERENCES `GROUP`(id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
;

ALTER TABLE TUTORSHIP
    ADD    FOREIGN KEY (student_id)
    REFERENCES `USER`(id)
;

ALTER TABLE TUTORSHIP
    ADD    FOREIGN KEY (ptutor_id)
    REFERENCES `USER`(id)
;

ALTER TABLE LIVRET
  ADD FOREIGN KEY (tutorship_id)
REFERENCES TUTORSHIP (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
;

ALTER TABLE LIVRET
  ADD FOREIGN KEY (etutor_id)
REFERENCES `USER` (id);
    
ALTER TABLE LIVRET
  ADD FOREIGN KEY (tutorship_id)
REFERENCES TUTORSHIP (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
;

ALTER TABLE PERIOD
    ADD FOREIGN KEY (livret_id)
REFERENCES LIVRET (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE;


# Create Indexes
CREATE UNIQUE INDEX user_email
  ON `USER` (`email`);
CREATE UNIQUE INDEX user_hash
  ON `USER` (`hash`);
