/* Unique Ids sequence generator */

drop sequence SEQ_GEN_SEQUENCE;
create sequence SEQ_GEN_SEQUENCE START WITH 1000 INCREMENT BY 50;


CREATE TABLE GROUP_ (
    id bigint NOT NULL PRIMARY KEY,
    groupName varchar(255) NOT NULL
);

CREATE TABLE USERGROUP (
    id bigint NOT NULL PRIMARY KEY,
    groupId bigint references GROUP_(id) on delete cascade,
    userId bigint references USERS(id)  on delete cascade
);
create index usergroup_groupId_idx ON USERGROUP(groupId);

CREATE TABLE GROUPPERMISSIONS (
    id bigint NOT NULL PRIMARY KEY,
    groupId bigint references GROUP_(id),
    serviceName varchar(255) not null,
    methodIndex int not null
);

/* METADATA */

CREATE TABLE attribute_group (
    id bigint NOT NULL PRIMARY KEY
);

CREATE TABLE attribute (
    id bigint NOT NULL PRIMARY KEY,
    name varchar(255),
    type varchar(255),
    value varchar(255),
    attribute_group_id bigint REFERENCES attribute_group (id),
    mandatory boolean
);

/* DATA */

CREATE TABLE gde_file (
    id bigint NOT NULL PRIMARY KEY,
    name varchar(255),
    length bigint,
    checksum varchar(255),
    creation_date timestamp,
    update_date timestamp,
    valid boolean,
    deleted boolean,
    deletion_date timestamp,
    attribute_group_id bigint REFERENCES attribute_group (id),
    data_profile_id bigint REFERENCES profile (id)
);

CREATE TABLE chunk (
    id bigint NOT NULL PRIMARY KEY,
    file_id bigint NOT NULL REFERENCES gde_file (id),
    rank bigint,
    checksum varchar(255),
    size bigint,
    data bytea
);

/* STUDIES */

CREATE TABLE study (
    id bigint NOT NULL PRIMARY KEY,
    name varchar(255),
    creation_date timestamp,
    update_date timestamp,
    valid boolean,
    deleted boolean,
    deletion_date timestamp,
    attribute_group_id bigint REFERENCES attribute_group (id),
    profile_id bigint REFERENCES profile (id)
);

/* PROFILES */

CREATE TABLE profile (
    id bigint NOT NULL PRIMARY KEY,
    name varchar(255)
);

CREATE TABLE profile_attribute (
    id bigint NOT NULL PRIMARY KEY,
    name varchar(255),
    type varchar(255),
    mandatory boolean,
    profile_id bigint REFERENCES profile (id)
);
INSERT INTO group_ (id,groupname) VALUES (1,'admins');
INSERT into usergroup(id,groupid,userid) VALUES (2,1,1);
INSERT INTO grouppermissions (id,groupid,servicename,methodname) VALUES (3, 1, 'UserService',1);
INSERT INTO grouppermissions (id,groupid,servicename,methodname) VALUES (3, 1, 'UserService',3); -- Add to group
INSERT INTO grouppermissions (id,groupid,servicename,methodname) VALUES (3, 1, 'UserService',4); -- Remove from group
INSERT INTO grouppermissions (id,groupid,servicename,methodname) VALUES (3, 1, 'UserService',5); -- Create group
INSERT INTO grouppermissions (id,groupid,servicename,methodname) VALUES (3, 1, 'UserService',6); -- Delete group
INSERT INTO grouppermissions (id,groupid,servicename,methodname) VALUES (3, 1, 'UserService',7); -- Find user
INSERT INTO grouppermissions (id,groupid,servicename,methodname) VALUES (3, 1, 'UserService',8); -- Find group

 
