-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied.
CREATE TABLE users
(
    ID       INTEGER PRIMARY KEY AUTOINCREMENT,
    Username TEXT UNIQUE NOT NULL
);

CREATE TABLE userlogin_provider
(
    ID         INTEGER PRIMARY KEY AUTOINCREMENT,
    user_ID    INTEGER     NOT NULL,
    login_stub TEXT UNIQUE NOT NULL,
    FOREIGN KEY (user_ID) REFERENCES users (ID)
);

CREATE TABLE user_sessions
(
    ID            INTEGER PRIMARY KEY AUTOINCREMENT,
    user_ID       INTEGER     NOT NULL,
    session_token TEXT UNIQUE NOT NULL,
    created_at    TEXT        NOT NULL,
    valid_until   TEXT        NOT NULL,
    name          TEXT,
    FOREIGN KEY (user_ID) REFERENCES users (ID)
);

CREATE TABLE storagespace
(
    ID       INTEGER PRIMARY KEY AUTOINCREMENT,
    Parent   INTEGER,
    Location TEXT,
    FOREIGN KEY (Parent) REFERENCES storagespace (ID)
);

CREATE TABLE objects
(
    ID              INTEGER PRIMARY KEY AUTOINCREMENT,
    storagespace_ID INTEGER NOT NULL,
    Name            TEXT    NOT NULL,
    Description     TEXT,
    Serialnumber    TEXT UNIQUE,
    created         DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (storagespace_ID) REFERENCES storagespace (ID)
);

CREATE TABLE events
(
    ID          INTEGER PRIMARY KEY AUTOINCREMENT,
    user_ID     INTEGER NOT NULL,
    Name        TEXT    NOT NULL,
    Description TEXT,
    Location    TEXT,
    Start_Date  TEXT    NOT NULL,
    End_Date    TEXT    NOT NULL,
    FOREIGN KEY (user_ID) REFERENCES users (ID)
);

CREATE TABLE pictures
(
    ID              INTEGER PRIMARY KEY AUTOINCREMENT,
    user_ID         INTEGER NOT NULL,
    storagespace_ID INTEGER,
    object_ID       INTEGER,
    event_ID        INTEGER,
    check_in_ID     INTEGER,
    Path            TEXT    NOT NULL,
    Description     TEXT,
    datetime        TEXT    NOT NULL,
    FOREIGN KEY (user_ID) REFERENCES users (ID),
    FOREIGN KEY (storagespace_ID) REFERENCES storagespace (ID),
    FOREIGN KEY (object_ID) REFERENCES objects (ID),
    FOREIGN KEY (event_ID) REFERENCES events (ID),
    FOREIGN KEY (check_in_ID) REFERENCES check_in (ID)
);

CREATE TABLE events_objects
(
    ID        INTEGER PRIMARY KEY AUTOINCREMENT,
    event_ID  INTEGER NOT NULL,
    object_ID INTEGER NOT NULL,
    FOREIGN KEY (event_ID) REFERENCES events (ID),
    FOREIGN KEY (object_ID) REFERENCES objects (ID)
);

CREATE TABLE checkin_events
(
    ID   INTEGER PRIMARY KEY AUTOINCREMENT,
    Name TEXT NOT NULL
);

CREATE TABLE checkin_states
(
    ID   INTEGER PRIMARY KEY AUTOINCREMENT,
    Name TEXT NOT NULL
);

CREATE TABLE check_in
(
    ID               INTEGER PRIMARY KEY AUTOINCREMENT,
    user_ID          INTEGER NOT NULL,
    checkin_event_ID INTEGER NOT NULL,
    event_ID         INTEGER,
    object_ID        INTEGER,
    checkin_state_ID INTEGER NOT NULL,
    datetime         TEXT    NOT NULL,
    FOREIGN KEY (user_ID) REFERENCES users (ID),
    FOREIGN KEY (checkin_event_ID) REFERENCES checkin_events (ID),
    FOREIGN KEY (event_ID) REFERENCES events (ID),
    FOREIGN KEY (object_ID) REFERENCES objects (ID),
    FOREIGN KEY (checkin_state_ID) REFERENCES checkin_states (ID)
);

CREATE TABLE annotations
(
    ID               INTEGER PRIMARY KEY AUTOINCREMENT,
    user_ID          INTEGER NOT NULL,
    object_ID        INTEGER,
    event_ID         INTEGER,
    check_in_ID      INTEGER,
    events_object_ID INTEGER,
    text             TEXT    NOT NULL,
    datetime         TEXT    NOT NULL,
    FOREIGN KEY (user_ID) REFERENCES users (ID),
    FOREIGN KEY (object_ID) REFERENCES objects (ID),
    FOREIGN KEY (event_ID) REFERENCES events (ID),
    FOREIGN KEY (check_in_ID) REFERENCES check_in (ID),
    FOREIGN KEY (events_object_ID) REFERENCES events_objects (ID)
);

-- +goose Down
-- SQL in section 'Down' is executed when this migration is rolled back.
DROP TABLE IF EXISTS annotations;
DROP TABLE IF EXISTS check_in;
DROP TABLE IF EXISTS checkin_states;
DROP TABLE IF EXISTS checkin_events;
DROP TABLE IF EXISTS events_objects;
DROP TABLE IF EXISTS pictures;
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS objects;
DROP TABLE IF EXISTS storagespace;
DROP TABLE IF EXISTS user_sessions;
DROP TABLE IF EXISTS userlogin_provider;
DROP TABLE IF EXISTS users;
