CREATE SCHEMA `up_and_down` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;

CREATE TABLE `up_and_down`.`users` (
    `uuid` CHAR(36) NOT NULL,
    `login` VARCHAR(128) UNIQUE NOT NULL,
    `hashed_password` TEXT NOT NULL,
    PRIMARY KEY (`uuid`),
    UNIQUE INDEX `uuid_UNIQUE` (`uuid` ASC)
);

CREATE TABLE `up_and_down`.`mania` (
	`level` INT1 NOT NULL,
    `description` TEXT NOT NULL,
    PRIMARY KEY (`level`)
);

CREATE TABLE `up_and_down`.`depressions` (
	`level` INT1 NOT NULL,
    `description` TEXT NOT NULL,
    PRIMARY KEY (`level`)
);

CREATE TABLE `up_and_down`.`moods` (
	`level` INT1 NOT NULL,
    `description` TEXT NOT NULL,
    PRIMARY KEY (`level`)
);

CREATE TABLE `up_and_down`.`activities` (
	`level` INT1 NOT NULL,
    `description` TEXT NOT NULL,
    PRIMARY KEY (`level`)
);

CREATE TABLE `up_and_down`.`appetites` (
	`level` INT1 NOT NULL,
    `description` TEXT NOT NULL,
    PRIMARY KEY (`level`)
);

CREATE TABLE `up_and_down`.`dreams` (
	`level` INT1 NOT NULL,
    `description` TEXT NOT NULL,
    PRIMARY KEY (`level`)
);

CREATE TABLE `up_and_down`.`anxiety` (
	`level` INT1 NOT NULL,
    `description` TEXT NOT NULL,
    PRIMARY KEY (`level`)
);

CREATE TABLE `up_and_down`.`user_treatment_schemes` (
    `uuid` CHAR(36) NOT NULL,
    `user_uuid` CHAR(36) NOT NULL,
    `treatment_name` TEXT NOT NULL,
    `instructions` TEXT,
    PRIMARY KEY (`uuid`),
    FOREIGN KEY (`user_uuid`) REFERENCES `users`(`uuid`)
);

CREATE TABLE `up_and_down`.`diaries` (
    `uuid` CHAR(36) NOT NULL,
    `user_uuid` CHAR(36) NOT NULL,
    `time` DATETIME NOT NULL,
    `mania_level` INT1 NOT NULL,
    `depression_level` INT1 NOT NULL,
    `mood_level` INT1 NOT NULL,
    `activity_level` INT1 NOT NULL,
    `appetite_level` INT1 NOT NULL,
    `dream_level` INT1 NOT NULL,
    `anxiety_level` INT1 NOT NULL,
    `comment` TEXT,
    `user_treatment_schemes_uuid` CHAR(36),
    PRIMARY KEY (`uuid`),
    FOREIGN KEY (`user_uuid`) REFERENCES `users`(`uuid`),
    FOREIGN KEY (`mania_level`) REFERENCES `mania`(`level`),
    FOREIGN KEY (`depression_level`) REFERENCES `depressions`(`level`),
    FOREIGN KEY (`mood_level`) REFERENCES `moods`(`level`),
    FOREIGN KEY (`activity_level`) REFERENCES `activities`(`level`),
    FOREIGN KEY (`appetite_level`) REFERENCES `appetites`(`level`),
    FOREIGN KEY (`dream_level`) REFERENCES `dreams`(`level`),
    FOREIGN KEY (`anxiety_level`) REFERENCES `anxiety`(`level`),
    FOREIGN KEY (`user_treatment_schemes_uuid`) REFERENCES `user_treatment_schemes`(`uuid`)
);

CREATE TABLE `up_and_down`.`medications` (
    `uuid` CHAR(36) NOT NULL,
    `name` TEXT NOT NULL,
    `dose` int8 NOT NULL,
    `unit` CHAR(30),
    `is_urgent` BOOL NOT NULL,
    PRIMARY KEY (`uuid`),
    UNIQUE KEY `uniq_medication` (`name`(255), `dose`, `unit`, `is_urgent`)
);

CREATE TABLE `up_and_down`.`treatment_schemes` (
	`user_treatment_schemes_uuid` CHAR(36) NOT NULL,
    `medication_uuid` CHAR(36) NOT NULL,
    PRIMARY KEY (`user_treatment_schemes_uuid`, `medication_uuid`),
    FOREIGN KEY (`user_treatment_schemes_uuid`) REFERENCES `user_treatment_schemes`(`uuid`),
    FOREIGN KEY (`medication_uuid`) REFERENCES `medications`(`uuid`)
);