-- Idempotent update: add Microsoft auto-remember and token TTL settings,
-- plus tenant-level auto_remember_mode column.

INSERT INTO `settings` (`key`, `value`, `description`)
VALUES
  ('microsoft_auto_remember_default', '0', 'setting.microsoft_auto_remember_default'),
  ('remember_token_lifetime_days', '30', 'setting.remember_token_lifetime_days')
ON DUPLICATE KEY UPDATE
  `key` = `key`;

-- Add auto_remember_mode to tenant_auth_microsoft (NULL = inherit global default).
SET @col_exists = (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'tenant_auth_microsoft'
    AND COLUMN_NAME = 'auto_remember_mode'
);
SET @sql = IF(@col_exists = 0,
  'ALTER TABLE `tenant_auth_microsoft` ADD COLUMN `auto_remember_mode` TINYINT(1) NULL DEFAULT NULL AFTER `client_secret_override_enc`',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
