-- 创建全局策略配置表（独立于账户）
-- 全局配置不依赖任何account_id，由管理员统一管理

CREATE TABLE IF NOT EXISTS `global_strategy_config` (
  `id` INT PRIMARY KEY AUTO_INCREMENT,
  `config_key` VARCHAR(100) NOT NULL,
  `config_value` TEXT NOT NULL,
  `config_type` VARCHAR(50) NOT NULL COMMENT 'string, number, boolean, json',
  `category` VARCHAR(50) NOT NULL COMMENT 'strategy, risk, scan',
  `description` TEXT,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updated_by` VARCHAR(50) COMMENT '更新人（用户名）',
  INDEX `idx_category` (`category`),
  UNIQUE KEY `uk_config_key` (`config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='全局策略配置表（管理员专用）';

-- 迁移现有account_id=1的核心策略配置到全局配置表
-- 注意：只迁移非风险旋钮的配置
INSERT INTO `global_strategy_config` (`config_key`, `config_value`, `config_type`, `category`, `description`)
SELECT 
  `config_key`,
  `config_value`,
  `config_type`,
  `category`,
  `description`
FROM `trading_config`
WHERE `account_id` = 1
  AND `config_key` NOT IN (
    'MIN_MARGIN_USDT',
    'MIN_POSITION_PERCENT',
    'MAX_POSITION_PERCENT',
    'MAX_TOTAL_POSITION_PERCENT',
    'AUTO_TRADE_ENABLED',
    'MAX_OPEN_POSITIONS',
    'MAX_DAILY_ENTRIES',
    'BINANCE_API_KEY',
    'BINANCE_API_SECRET',
    'USE_TESTNET'
  )
ON DUPLICATE KEY UPDATE
  `config_value` = VALUES(`config_value`),
  `config_type` = VALUES(`config_type`),
  `category` = VALUES(`category`),
  `description` = VALUES(`description`),
  `updated_at` = CURRENT_TIMESTAMP;
