-- noinspection SqlNoDataSourceInspectionForFile

-- Schema: alerts
-- Brief: Alerts component controller
-- Created by: gmedeiros
-- Table schema_version: must be defined for database update through the AlertsPersistence component

CREATE TABLE schema_version AS
SELECT "$Author: gmedeiros $" AS Author,
       "$Date: 2022-10-31 10:00:00 -0300$" AS LastModifiedAt,
       "$Revision:$" AS Revision;

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE TABLE ConfiguredAlerts (
  Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  AlertJson VARCHAR
);

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE TABLE StatusType (
    TypeId INTEGER NOT NULL PRIMARY KEY,
    TypeDescr VARCHAR(50) NOT NULL
);

INSERT INTO StatusType (TypeId, TypeDescr) VALUES (-2, 'Error');
INSERT INTO StatusType (TypeId, TypeDescr) VALUES (-1, 'Interrupted Event');
INSERT INTO StatusType (TypeId, TypeDescr) VALUES (0, 'Not sent');
INSERT INTO StatusType (TypeId, TypeDescr) VALUES (1, 'Sent');

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

CREATE TABLE Events (
  Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  AlertId INTEGER NOT NULL,
  NotificationJson VARCHAR,
  Timestamp DATETIME NOT NULL,
  SentStatus INTEGER NOT NULL CONSTRAINT FK_Events_StatusType REFERENCES StatusType(TypeId),
  FOREIGN KEY(AlertId) REFERENCES ConfiguredAlerts(Id)
);

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
