/*
 * advotracker - Hotline tackingtool for Advocats
 *
 * Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
 * SPDX-License-Identifier: (0BSD, MIT)
 */

-- echo ".read <this_file> |
--      sqlite3 advotracker.db \

CREATE TABLE IF NOT EXISTS claims (
       id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
       id_user INTEGER  NOT NULL,
       type VARCHAR,
       value VARCHAR,
       date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       id_user_changed INTEGER NOT NULL,
       date_changed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       -- CONSTRAINTS
       CONSTRAINT claims_fk_id_user FOREIGN KEY (id_user)
		  REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE INDEX IF NOT EXISTS claims_ix_id_user ON claims (id_user);

CREATE TABLE IF NOT EXISTS harms (
       id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
       id_harm VARCHAR NOT NULL,
       id_policyholder VARCHAR,
       id_callback VARCHAR,
       date_fallback TIMESTAMP,
       date_recording TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       id_user INTEGER NOT NULL,
       date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       id_user_changed INTEGER NOT NULL,
       date_changed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS harms_ix_number_harm ON harms (id_harm);

CREATE TABLE IF NOT EXISTS roles (
       id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
       name VARCHAR NOT NULL,
       date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       id_user_changed INTEGER NOT NULL,
       date_changed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       -- Constraints
       CONSTRAINT user_roles_uk_name UNIQUE (name)
);

CREATE TABLE IF NOT EXISTS users (
       id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
       last_name VARCHAR NOT NULL,
       first_name VARCHAR,
       alias_name VARCHAR,
       email VARCHAR,
       /* email_confirmed boolean NOT NULL DEFAULT 0, */
       email_confirmed INTEGER,
       initials VARCHAR,
       password_hash VARCHAR,
       date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
       id_changed Integer NOT NULL,
       date_changed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
       -- Constraints
       CONSTRAINT users_ck_email_confirmed CHECK (email_confirmed IN (0,1))
);
CREATE INDEX IF NOT EXISTS users_ix_email ON users (email);

CREATE TABLE IF NOT EXISTS user_roles (
       /* id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, */
       id_user INTEGER NOT NULL,
       id_role INTEGER NOT NULL,
       -- Constraints
       CONSTRAINT user_roles_pk_id_user_id_role PRIMARY KEY (id_user, id_role),
       CONSTRAINT user_roles_fk_id_user FOREIGN KEY (id_user)
	   REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE,
	   CONSTRAINT user_roles_fk_id_role FOREIGN KEY (id_role)
	   REFERENCES roles (id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE INDEX IF NOT EXISTS user_roles_ix_id_user ON user_roles (id_user);
CREATE INDEX IF NOT EXISTS user_roles_ix_id_role ON user_roles (id_role);

CREATE VIEW IF NOT EXISTS view_user_roles
       AS SELECT
       users.id_user AS id_user,
       users.last_name AS last_name,
       users.first_name AS first_name,
       users.email AS email,
       users.user_initials AS initials,
       role.user_rolename AS role_name
       FROM user_roles
       INNER JOIN users ON users.id_user = user_roles.id_user
       INNER JOIN roles ON user_roles.id = user_roles.id_role;

-- end
