CREATE TYPE role AS ENUM ('staff', 'areamanager', 'admin');
CREATE TYPE function AS ENUM ('posten', 'fuehrungsassistent', 'wachhabender');

CREATE TABLE area
(
    id              SERIAL          PRIMARY KEY,
    name            TEXT            NOT NULL
);

INSERT INTO area (name) VALUES ('Leipzig Ost');

CREATE TABLE location
(
    id              SERIAL          PRIMARY KEY,
    name            TEXT            NOT NULL,
    areaId          INTEGER         NOT NULL REFERENCES area (id) ON DELETE CASCADE
);

CREATE TABLE user_
(
    id                      SERIAL          PRIMARY KEY,
    name                    TEXT            NOT NULL,
    email                   TEXT            NOT NULL,
    password                TEXT            ,
    salt                    TEXT            ,
    role                    role            NOT NULL,
    function                function        NOT NULL,
    areaId                  INTEGER         NOT NULL REFERENCES area (id) ON DELETE CASCADE,
    locked                  BOOLEAN         NOT NULL DEFAULT false,
    lastLogin               TIMESTAMP WITH TIME ZONE,
    receiveNotifications    BOOLEAN         NOT NULL DEFAULT true
);

CREATE TABLE availabillity
(
    id                      SERIAL          PRIMARY KEY,
    userId                  INTEGER         NOT NULL REFERENCES user_ (id) ON DELETE CASCADE,
    date                    DATE            NOT NULL,
    startTime               TIME,
    endTime                 TIME,
    comment                 TEXT
);

CREATE TABLE event
(
    id                      SERIAL          PRIMARY KEY,
    date                    DATE            NOT NULL,
    startTime               TIME            NOT NULL,
    endTime                 TIME            NOT NULL,
    name                    TEXT            NOT NULL,
    locationId              INTEGER         NOT NULL REFERENCES location (id) ON DELETE CASCADE,
    voluntaryWachhabender           BOOLEAN         NOT NULL,
    voluntaryFuehrungsassistent     BOOLEAN         NOT NULL,
    amountOfPosten          SMALLINT        NOT NULL CHECK (amountOfPosten >= 0),
    clothing                TEXT            NOT NULL,
    note                    TEXT,
    canceled                BOOLEAN         NOT NULL DEFAULT false
);

CREATE TABLE assignment
(
    eventId                 INTEGER         REFERENCES event (id) ON DELETE CASCADE,
    availabillityId         INTEGER         REFERENCES availabillity (id) ON DELETE CASCADE,
    function                function        NOT NULL,
    startTime               TIME            NOT NULL,
    endTime                 TIME            NOT NULL,
    PRIMARY KEY (eventId, availabillityId)
);

CREATE TABLE vehicle
(
    id                      SERIAL          PRIMARY KEY,
    radioCallName           TEXT            NOT NULL,
    station                 TEXT            NOT NULL
);

CREATE TABLE vehicleassignement
(
    eventId                 INTEGER         REFERENCES event (id) ON DELETE CASCADE,
    vehicleId               INTEGER         REFERENCES vehicle (id) ON DELETE CASCADE,
    PRIMARY KEY (eventId, vehicleId)
);

CREATE UNLOGGED TABLE session
(
    id                      SERIAL          PRIMARY KEY,
    key                     TEXT            UNIQUE NOT NULL,
    sessionstate            JSONB,
    expires                 TIMESTAMP
);

CREATE INDEX session_key_idx ON session (key);

CREATE UNLOGGED TABLE passwordReset
(
    id                      SERIAL          PRIMARY KEY,
    token                   TEXT            UNIQUE NOT NULL,
    userId                  INTEGER         NOT NULL REFERENCES user_ (id) ON DELETE CASCADE,
    expires                 TIMESTAMP       NOT NULL
);

CREATE INDEX passwordReset_token_idx ON passwordReset (token);

CREATE UNLOGGED TABLE registration
(
    id                      SERIAL          PRIMARY KEY,
    token                   TEXT            UNIQUE NOT NULL,
    userId                  INTEGER         NOT NULL REFERENCES user_ (id) ON DELETE CASCADE,
    expires                 TIMESTAMP       NOT NULL
);

CREATE INDEX registration_token_idx ON registration (token);
