CREATE TABLE IF NOT EXISTS `wp_tmapp_sessions` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL,
  `token_hash` CHAR(64) NOT NULL,
  `user_agent` TEXT DEFAULT NULL,
  `ip` VARCHAR(64) DEFAULT NULL,
  `expires_at` DATETIME NOT NULL,
  `revoked_at` DATETIME DEFAULT NULL,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `token_hash` (`token_hash`),
  KEY `user_id` (`user_id`),
  KEY `expires_at` (`expires_at`),
  KEY `revoked_at` (`revoked_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

ALTER TABLE `wp_tmapp_sessions` ADD COLUMN IF NOT EXISTS `user_agent` TEXT DEFAULT NULL;
ALTER TABLE `wp_tmapp_sessions` ADD COLUMN IF NOT EXISTS `ip` VARCHAR(64) DEFAULT NULL;
ALTER TABLE `wp_tmapp_sessions` ADD COLUMN IF NOT EXISTS `expires_at` DATETIME NULL DEFAULT NULL;
ALTER TABLE `wp_tmapp_sessions` ADD COLUMN IF NOT EXISTS `revoked_at` DATETIME DEFAULT NULL;
ALTER TABLE `wp_tmapp_sessions` ADD COLUMN IF NOT EXISTS `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE `wp_tmapp_sessions` ADD UNIQUE KEY IF NOT EXISTS `token_hash` (`token_hash`);
ALTER TABLE `wp_tmapp_sessions` ADD KEY IF NOT EXISTS `user_id` (`user_id`);
ALTER TABLE `wp_tmapp_sessions` ADD KEY IF NOT EXISTS `expires_at` (`expires_at`);
ALTER TABLE `wp_tmapp_sessions` ADD KEY IF NOT EXISTS `revoked_at` (`revoked_at`);
