CREATE TABLE IF NOT EXISTS users_sessions (
    id_session INT UNSIGNED NOT NULL AUTO_INCREMENT,
    id_user INT UNSIGNED NOT NULL,
    token_hash CHAR(64) NOT NULL,
    ip VARCHAR(45) NULL,
    user_agent VARCHAR(255) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    last_seen DATETIME NULL,
    expires_at DATETIME NOT NULL,
    revoked TINYINT UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (id_session),
    UNIQUE KEY uniq_token (token_hash),
    KEY idx_user (id_user),
    KEY idx_active (id_user, revoked, expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
