--creating system table
CREATE TABLE IF NOT EXISTS system (
    `key` VARCHAR(32) PRIMARY KEY,
    `value` TEXT
);

--creating roles table
CREATE TABLE IF NOT EXISTS roles (
    `id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(256) UNIQUE NOT NULL,
    `color` INTEGER UNSIGNED DEFAULT 0
);

--creating accounts table
CREATE TABLE IF NOT EXISTS accounts (
    `id` INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `login` VARCHAR(256) UNIQUE NOT NULL,
    `nick` VARCHAR(256),
    `type` INTEGER UNSIGNED NOT NULL,
    `password` VARCHAR(64),
    `salt` VARCHAR(32),
    `role` INTEGER UNSIGNED NOT NULL,
    `created` TIMESTAMP DEFAULT UTC_TIMESTAMP(),

    FOREIGN KEY (role) REFERENCES roles(id)
);

--creating defailt roles
INSERT IGNORE INTO roles (`name`)
VALUES ('root');

--inserting initial version
INSERT INTO system (`key`, `value`) VALUES ('version', '0');

--recording initial time
INSERT INTO system (`key`, `value`) VALUES ('created', UTC_TIMESTAMP());
