CREATE TABLE login_attempts (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    username   TEXT NOT NULL,
    password   TEXT NOT NULL,
    ip         TEXT NOT NULL,
    count      INTEGER NOT NULL DEFAULT 1,
    first_seen TEXT NOT NULL,
    last_seen  TEXT NOT NULL,
    UNIQUE(username, password, ip)
);

CREATE INDEX idx_login_attempts_last_seen ON login_attempts(last_seen);
CREATE INDEX idx_login_attempts_ip ON login_attempts(ip);

CREATE TABLE sessions (
    id              TEXT PRIMARY KEY,
    ip              TEXT NOT NULL,
    username        TEXT NOT NULL,
    shell_name      TEXT NOT NULL DEFAULT '',
    connected_at    TEXT NOT NULL,
    disconnected_at TEXT,
    human_score     REAL
);

CREATE INDEX idx_sessions_connected_at ON sessions(connected_at);

CREATE TABLE session_logs (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
    timestamp  TEXT NOT NULL,
    input      TEXT NOT NULL DEFAULT '',
    output     TEXT NOT NULL DEFAULT ''
);

CREATE INDEX idx_session_logs_session_id ON session_logs(session_id);
CREATE INDEX idx_session_logs_timestamp ON session_logs(timestamp);
