-- noinspection SqlNoDataSourceInspectionForFile
CREATE TABLE IF NOT EXISTS servers (
    id TEXT NOT NULL,
    name TEXT NOT NULL,
    address TEXT NOT NULL,
    rcon_address TEXT NOT NULL,
    rcon_password TEXT NOT NULL,
    owner TEXT NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS users (
    id TEXT NOT NULL,
    token TEXT NOT NULL,
    display_name TEXT NOT NULL,
    refresh_token TEXT NOT NULL,
    token_expiry TEXT NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE IF NOT EXISTS sessions (
    token TEXT NOT NULL,
    uid TEXT NOT NULL,
    expiry TEXT NOT NULL,
    PRIMARY KEY (token),
    FOREIGN KEY (uid) REFERENCES users(id)
);

CREATE TABLE IF NOT EXISTS oauth_states (
    state_id TEXT NOT NULL,
    origin TEXT NOT NULL,
    state TEXT NOT NULL,
    PRIMARY KEY (state_id)
);

CREATE TABLE IF NOT EXISTS invites (
    token TEXT NOT NULL,
    creator TEXT NOT NULL,
    server TEXT NOT NULL,
    uses INT NOT NULL,
    unlimited BOOLEAN NOT NULL,
    PRIMARY KEY (token),
    FOREIGN KEY (server) REFERENCES servers(id),
    FOREIGN KEY (creator) REFERENCES users(id)
);

CREATE TABLE IF NOT EXISTS invite_log (
    entry_id TEXT NOT NULL,
    invite TEXT NOT NULL,
    uid TEXT NOT NULL,
    PRIMARY KEY (entry_id),
    FOREIGN KEY (invite) REFERENCES invites(token),
	FOREIGN KEY (uid) REFERENCES users(id)
);
