/**
 * Author:  joern.muehlencord
 * Created: 06.09.2015
 */

DROP TABLE account_role;
DROP TABLE account_history;
DROP TABLE account;
DROP TABLE role_permission;
DROP TABLE application_role;
DROP TABLE application_permission;


CREATE TABLE application_role (
  id UUID NOT NULL,
  role_name varchar(80) NOT NULL,
  role_description varchar(200) NOT NULL,
  CONSTRAINT pk_application_role_pk PRIMARY KEY (id),
  CONSTRAINT uidx_application_id UNIQUE (id)
);
 
CREATE TABLE account (
  id UUID NOT NULL,
  username varchar(32) NOT NULL,    
  emailaddress varchar(200) NOT NULL,   
  firstname varchar(100) NOT NULL,
  lastname varchar(100) NOT NULL,  
  account_password char(200) NOT NULL,  
  last_login timestamp with time zone,
  last_failed_login timestamp with time zone,
  failure_count int NOT NULL DEFAULT 0,
  status varchar(10) NOT NULL DEFAULT 'NEW', -- NEW, INIT, OK, BLOCKED,
  password_reset_ongoing boolean NOT NULL DEFAULT false,
  password_reset_valid_to timestamp with time zone,
  password_reset_hash char(200),
  created_on timestamp with time zone NOT NULL DEFAULT (now() at time zone 'utc'),
  created_by varchar(32) NOT NULL,
  last_updated_on timestamp with time zone NOT NULL DEFAULT (now() at time zone 'utc'),
  last_updated_by varchar(32) NOT NULL,
  CONSTRAINT pk_account PRIMARY KEY (id),
  CONSTRAINT uidx_username UNIQUE (username)
);

CREATE TABLE account_history (
  id UUID NOT NULL,
  account_id UUID NOT NULL,
  message varchar(200),
  failure_count int NOT NULL DEFAULT 0,
  status varchar(20) NOT NULL, -- constants needed, after action - new, init, active, blocked, inactive, marked for deletion
  last_updated_on timestamp with time zone NOT NULL DEFAULT (now() at time zone 'utc'),
  last_updated_by varchar(32) NOT NULL,
  CONSTRAINT pk_account_history PRIMARY KEY (id),
  CONSTRAINT fk_account_history_username_fk FOREIGN KEY (account_id) REFERENCES account (id)
);

CREATE TABLE account_role (
  account UUID NOT NULL,
  account_role UUID NOT NULL,  
  CONSTRAINT pk_account_role PRIMARY KEY (account, account_role),
  CONSTRAINT fk_account_role_account FOREIGN KEY (account) REFERENCES account(id),
  CONSTRAINT fk_account_role_role_name FOREIGN KEY (account_role) REFERENCES application_role(id)
);


CREATE TABLE application_permission (
  id UUID NOT NULL,
  permission_name varchar(80) NOT NULL,
  permission_description varchar(200) NOT NULL,
  CONSTRAINT pk_application_permission PRIMARY KEY (id),
  CONSTRAINT uidx_application_permission_name UNIQUE (permission_name)
);

CREATE TABLE role_permission (
  application_role UUID NOT NULL,
  role_permission UUID NOT NULL,
  CONSTRAINT pk_role_permission_role_permission_name PRIMARY KEY (application_role, role_permission),
  CONSTRAINT fk_role_permission_application_role FOREIGN KEY (application_role) REFERENCES application_role(id),
  CONSTRAINT fk_role_permission_role_permission FOREIGN KEY (role_permission) REFERENCES application_permission(id)  
);

INSERT INTO application_permission (id, permission_name, permission_description) values ('dfd0f8f1-4a51-4fdc-9a1c-a942bee9b649', 'test:view', 'Display test view');

INSERT INTO application_role (id, role_name, role_description) values ('5cd0aca0-5466-483d-8f3e-c369f8061131','Admin', 'Admin role');
INSERT INTO application_role (id, role_name, role_description) values ('da30060e-fd23-4016-a506-4e12e9322148', 'User', 'Standard user role');

-- INSERT INTO role_permission (role_name, permission_name) values ('Admin','test:view');

INSERT INTO account (id, username, emailaddress, firstname, lastname, account_password, created_by, last_updated_by) values('ab5c8337-6872-4aea-a9b9-78ea63706b8f','admin', 'joern@muehlencord.de', 'Joern', 'Muehlencord','$shiro1$SHA-256$500000$4bHPNH9k539UjdFLgm/HOA==$T/n8skgoGSOtNw/c9ScDlXCiGrx2cZF0Esrvf6WPq6g=', 'admin','admin'); --admin/secret
INSERT INTO account_role (account, account_role) values ('ab5c8337-6872-4aea-a9b9-78ea63706b8f', '5cd0aca0-5466-483d-8f3e-c369f8061131');

--select uuid_generate_v4();