--------------------------------------------------------------------
-- Current version of the DB is the highest value of filename
-- in the folder `sqldelight/databases`.
--
-- When upgrading the schema, you have to create a file .sqm in the
-- `sqldelight/databases` folder and run the following task to
-- generate a .db file using the latest schema
-- > ./gradlew generateDebugSessionDatabaseSchema
--------------------------------------------------------------------

CREATE TABLE SessionData (
    userId TEXT NOT NULL PRIMARY KEY,
    deviceId TEXT NOT NULL,
    accessToken TEXT NOT NULL,
    refreshToken TEXT,
    homeserverUrl TEXT NOT NULL,
    -- added in version 2
    loginTimestamp INTEGER,
    -- added in version 3
    oidcData TEXT,
    -- added in version 4
    isTokenValid INTEGER NOT NULL DEFAULT 1,
    loginType TEXT,
    -- added in version 5
    passphrase TEXT,
    -- added in version 6
    sessionPath TEXT NOT NULL DEFAULT "",
    -- added in version 9
    cachePath TEXT NOT NULL DEFAULT "",
    -- added in version 10
    -- position, to be able to sort account by session creation date
    position INTEGER NOT NULL DEFAULT 0,
    -- index of the last usage session. Each time the current session change, the index of the current
    -- session is incremented to the max value + 1 so it becomes the current session
    lastUsageIndex INTEGER NOT NULL DEFAULT 0,
    -- user display name
    userDisplayName TEXT,
    -- user avatar url
    userAvatarUrl TEXT
);


selectLatest:
SELECT * FROM SessionData ORDER BY lastUsageIndex DESC LIMIT 1;

selectAll:
SELECT * FROM SessionData ORDER BY lastUsageIndex DESC;

count:
SELECT count(*) FROM SessionData;

selectByUserId:
SELECT * FROM SessionData WHERE userId = ?;

insertSessionData:
INSERT INTO SessionData VALUES ?;

removeSession:
DELETE FROM SessionData WHERE userId = ?;

updateSession:
REPLACE INTO SessionData VALUES ?;
