CREATE TABLE IF NOT EXISTS Players (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    is_admin INTEGER NOT NULL DEFAULT 0,
    name VARCHAR NOT NULL,
    pwd_hash VARCHAR NOT NULL,
    picture VARCHAR
);

CREATE TABLE IF NOT EXISTS Truths (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    week INTEGER NOT NULL CHECK (week > 0),
    number INTEGER NOT NULL CHECK (number > 0),
    author_id INTEGER NOT NULL,
    rendered_text VARCHAR NOT NULL,
    raw_text VARCHAR NOT NULL,
    FOREIGN KEY (author_id) REFERENCES Players(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS Votes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    truth_id INTEGER NOT NULL,
    voter_id INTEGER NOT NULL,
    voted_id INTEGER NOT NULL,
    FOREIGN KEY (truth_id) REFERENCES Truths(id) ON DELETE CASCADE,
    FOREIGN KEY (voter_id) REFERENCES Players(id) ON DELETE CASCADE,
    FOREIGN KEY (voted_id) REFERENCES Players(id) ON DELETE CASCADE
 );

CREATE TABLE IF NOT EXISTS AuthTokens (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    player_id INTEGER NOT NULL,
    token VARCHAR NOT NULL,
    max_timestamp INTEGER NOT NULL,
    FOREIGN KEY (player_id) REFERENCES Players(id) ON DELETE CASCADE
);
