CALL PR_DROP_IF_EXIST('CRITERIA_VAL');
CALL PR_DROP_IF_EXIST('CRITERIA');
CALL PR_DROP_IF_EXIST('MEM_MONITOR');
CALL PR_DROP_IF_EXIST('REP_USER');
CALL PR_DROP_IF_EXIST('REPORT_OWNER');
CALL PR_DROP_IF_EXIST('CRITERIA');
CALL PR_DROP_IF_EXIST('REPORT');
CALL PR_DROP_IF_EXIST('REPORT_OWNER');
CALL PR_DROP_IF_EXIST('REPORT_TYPE');
CALL PR_DROP_IF_EXIST('SCHEDULING_REPORT');
CALL PR_DROP_IF_EXIST('SCHEDULING_CRITERIA_VAL');

CREATE TABLE CRITERIA
(
  CRITERIA_ID    NUMBER(19)                     NOT NULL PRIMARY KEY,
  CRITERIA_NAME  VARCHAR2(32 CHAR)              NOT NULL,
  CRITERIA_TYPE  VARCHAR2(32 CHAR)              NOT NULL, 
  IS_TEMPOREL    VARCHAR2(5 CHAR)               NOT NULL
);

CREATE TABLE CRITERIA_VAL
(
  CRITERIA_VAL_ID  NUMBER(19)                   NOT NULL PRIMARY KEY,
  REPORT_ID        NUMBER(19),
  CRITERIA_ID      NUMBER(19)                   NOT NULL,
  CRITERIA_VALUE   VARCHAR2(3999 CHAR)
);

CREATE TABLE MEM_MONITOR
(
  MEM_MONITOR_ID  NUMBER(19)                    NOT NULL PRIMARY KEY,
  MONITORDATE     TIMESTAMP(6),
  TOTALMEMORY     FLOAT(126),
  MAXMEMORY       FLOAT(126),
  FREEMEMORY      FLOAT(126),
  USEDMEMORY      FLOAT(126),
  MAXPERCENT      FLOAT(126)
);

CREATE TABLE REP_USER
(
  USER_ID             NUMBER(19)                NOT NULL PRIMARY KEY,
  LOGIN               VARCHAR2(16 CHAR)         NOT NULL,
  EMAIL               VARCHAR2(255 CHAR),
  SURNAME             VARCHAR2(32 CHAR),
  FIRSTNAME           VARCHAR2(32 CHAR),
  LASTCONNECTIONDATE  DATE, 
  IS_ADMIN            VARCHAR2(5 CHAR)
);

CREATE TABLE REPORT
(
  REPORT_ID            NUMBER(19)               NOT NULL PRIMARY KEY,
  STARTGENERATIONDATE  TIMESTAMP(6),
  ENDGENERATIONDATE    TIMESTAMP(6),
  GENERATION_DURATION  NUMBER(19),
  REPORT_CODE          VARCHAR2(3 CHAR)         NOT NULL,
  REPORTUID            VARCHAR2(64 CHAR)        NOT NULL,
  FILE_NAME            VARCHAR2(255 CHAR),
  STATE                VARCHAR2(16 CHAR)        NOT NULL,
  ERROR_TYPE           VARCHAR2(16 CHAR),
  ERROR_MESSAGE        VARCHAR2(3999 CHAR), 
  SCHEDULING_REPORT_ID NUMBER(19,0)
);

CREATE TABLE REPORT_OWNER
(
  REPORT_OWNER_ID      NUMBER(19)               NOT NULL PRIMARY KEY,
  REPORT_ID            NUMBER(19)               NOT NULL,
  USER_ID              NUMBER(19)               NOT NULL,
  REQUESTDATE          TIMESTAMP(6),
  MONTH_PERIOD         VARCHAR2(7 CHAR)
);

CREATE TABLE REPORT_TYPE
(
  REPORT_TYPE_ID               NUMBER(19)                    NOT NULL PRIMARY KEY,
  CODE                         VARCHAR2(3 CHAR)              NOT NULL,
  TITLE                        VARCHAR2(255 CHAR)            NOT NULL,
  REVISION_RULE                VARCHAR2(255 CHAR),
  PRIORITY                     NUMBER(19)                    NOT NULL, 
  REPORT_AUTHORIZED            VARCHAR2(5 CHAR), 
  IS_CANDIDATE_FOR_SCHEDULING  VARCHAR2(5 CHAR)
);

CREATE TABLE SCHEDULING_CRITERIA_VAL
(
  SCHEDULING_CRITERIA_VAL_ID   NUMBER(19)                    NOT NULL PRIMARY KEY,
  CRITERIA_ID                  NUMBER(19)                    NOT NULL,
  SCHEDULING_REPORT_ID         NUMBER(19)                    NOT NULL,
  CRITERIA_VALUE               VARCHAR2(32 CHAR)
);

CREATE TABLE SCHEDULING_REPORT
(
  SCHEDULING_REPORT_ID         NUMBER(19)                    NOT NULL PRIMARY KEY,
  SCHEDULING_NAME              VARCHAR2(200 CHAR)            NOT NULL,
  USER_ID                      NUMBER(19)                    NOT NULL,
  LAST_UPDATE                  TIMESTAMP(6),
  REPORT_CODE                  VARCHAR2(3 CHAR)              NOT NULL
);
