-- PK10 极速赛车平台 完整数据库初始化
-- 兼容 MySQL 5.7+
-- 包含所有基础表 + PK10扩展表，一次执行即可

-- ========== 基础表 ==========

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(100) NOT NULL DEFAULT '',
  `avatar` varchar(128) DEFAULT NULL,
  `balance` decimal(15,2) NOT NULL DEFAULT '0.00',
  `status` int(11) NOT NULL DEFAULT '0',
  `role` varchar(20) NOT NULL DEFAULT 'user',
  `email_verified` tinyint(1) DEFAULT 0 COMMENT '邮箱是否验证',
  `email_token` varchar(64) DEFAULT NULL,
  `email_token_expire` datetime DEFAULT NULL,
  `usdt_address` varchar(64) DEFAULT NULL COMMENT 'USDT TRC20地址',
  `usdt_chain` varchar(10) DEFAULT 'TRC20',
  `is_virtual` tinyint(1) DEFAULT 0 COMMENT '虚拟账户标记',
  `agent_id` int(11) DEFAULT NULL COMMENT '所属代理ID',
  `lang` varchar(5) DEFAULT 'en' COMMENT '语言偏好',
  `invite_code` varchar(16) DEFAULT NULL COMMENT '邀请码',
  `rebate_rate` decimal(5,2) DEFAULT 0 COMMENT '反水比例%',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `games` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '游戏ID',
  `name` varchar(255) NOT NULL COMMENT '游戏名称',
  `code` varchar(100) NOT NULL COMMENT '游戏标识',
  `description` text,
  `stream_url` text COMMENT '直播流地址',
  `type` varchar(50) DEFAULT 'dice' COMMENT '游戏类型',
  `tags` text,
  `image` varchar(255) DEFAULT NULL,
  `thumbnail` varchar(255) DEFAULT NULL,
  `sort_order` int(11) NOT NULL DEFAULT '0',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `featured` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `game_odds` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `game_id` int(11) NOT NULL,
  `type` varchar(50) NOT NULL,
  `target` varchar(50) NOT NULL DEFAULT 'all',
  `odds` decimal(10,2) NOT NULL DEFAULT '0.00',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_game_bet` (`game_id`,`type`,`target`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `periods` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `period_number` varchar(50) NOT NULL,
  `status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT 'pending/locked/drawn/settled',
  `game_id` int(11) DEFAULT NULL,
  `dice1` tinyint(1) DEFAULT NULL,
  `dice2` tinyint(1) DEFAULT NULL,
  `dice3` tinyint(1) DEFAULT NULL,
  `total` tinyint(2) DEFAULT NULL,
  `result` varchar(200) DEFAULT NULL,
  `stream_url` text,
  `start_time` datetime DEFAULT NULL,
  `end_time` datetime DEFAULT NULL,
  `draw_time` datetime DEFAULT NULL,
  `created_by` int(11) DEFAULT NULL,
  `approved_by` int(11) DEFAULT NULL,
  `auto_generated` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_period_number` (`period_number`),
  KEY `idx_status` (`status`),
  KEY `idx_game_id` (`game_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bets` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `game_id` int(11) DEFAULT NULL,
  `period_id` int(11) NOT NULL,
  `period_number` varchar(50) NOT NULL,
  `bet_type` varchar(20) NOT NULL,
  `bet_value` varchar(50) DEFAULT NULL,
  `amount` decimal(15,2) NOT NULL DEFAULT '0.00',
  `odds` decimal(10,2) NOT NULL DEFAULT '0.00',
  `status` varchar(20) NOT NULL DEFAULT 'pending',
  `win_amount` decimal(15,2) DEFAULT '0.00',
  `commission` decimal(15,2) DEFAULT '0.00',
  `is_virtual` tinyint(1) DEFAULT 0 COMMENT '虚拟账户投注',
  `agent_id` int(11) DEFAULT NULL COMMENT '所属代理',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `settled_at` datetime DEFAULT NULL,
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_period_id` (`period_id`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `transactions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `type` varchar(20) NOT NULL,
  `amount` decimal(15,2) NOT NULL DEFAULT '0.00',
  `balance_before` decimal(15,2) NOT NULL DEFAULT '0.00',
  `balance_after` decimal(15,2) NOT NULL DEFAULT '0.00',
  `related_id` int(11) DEFAULT NULL,
  `description` text,
  `is_virtual` tinyint(1) DEFAULT 0 COMMENT '虚拟账户交易',
  `operator_id` int(11) DEFAULT NULL COMMENT '操作员工ID',
  `operator_type` varchar(16) DEFAULT NULL COMMENT 'admin/employee/agent',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `images` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `newname` varchar(255) NOT NULL,
  `url` text NOT NULL,
  `size` int(11) NOT NULL,
  `width` int(11) NOT NULL,
  `height` int(11) NOT NULL,
  `upload_time` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `plugins` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` text,
  `version` varchar(20) DEFAULT NULL,
  `author` varchar(100) DEFAULT NULL,
  `url` varchar(128) NOT NULL DEFAULT '',
  `status` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `system_settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `setting_key` varchar(100) NOT NULL,
  `setting_value` text,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_setting_key` (`setting_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ========== PK10 扩展表 ==========

CREATE TABLE IF NOT EXISTS `agents` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `user_id` int(11) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `level` tinyint(4) DEFAULT 1 COMMENT '1=总代 2=代理',
  `agent_code` varchar(16) UNIQUE NOT NULL,
  `commission_rate` decimal(5,2) DEFAULT 0,
  `rebate_rate` decimal(5,2) DEFAULT 0,
  `status` tinyint(4) DEFAULT 1,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  KEY `idx_parent` (`parent_id`),
  KEY `idx_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `agent_odds` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `agent_id` int(11) NOT NULL,
  `game_id` int(11) NOT NULL,
  `bet_type` varchar(32) NOT NULL,
  `bet_target` varchar(32) DEFAULT 'all',
  `odds` decimal(10,4) NOT NULL,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `uk_agent_game_type` (`agent_id`,`game_id`,`bet_type`,`bet_target`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `employees` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `emp_code` varchar(16) UNIQUE NOT NULL,
  `username` varchar(64) UNIQUE NOT NULL,
  `password` varchar(255) NOT NULL,
  `real_name` varchar(64) DEFAULT NULL,
  `shift` enum('day','night','all') DEFAULT 'all',
  `permissions` json DEFAULT NULL,
  `status` tinyint(4) DEFAULT 1,
  `last_login` datetime DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `employee_logs` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `emp_id` int(11) NOT NULL,
  `action` varchar(32) NOT NULL,
  `target_user_id` int(11) NOT NULL,
  `amount` decimal(14,2) NOT NULL,
  `remark` varchar(255) DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  KEY `idx_emp` (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `pk10_results` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `period_id` int(11) NOT NULL,
  `rank_1` tinyint(4) NOT NULL,
  `rank_2` tinyint(4) NOT NULL,
  `rank_3` tinyint(4) NOT NULL,
  `rank_4` tinyint(4) NOT NULL,
  `rank_5` tinyint(4) NOT NULL,
  `rank_6` tinyint(4) NOT NULL,
  `rank_7` tinyint(4) NOT NULL,
  `rank_8` tinyint(4) NOT NULL,
  `rank_9` tinyint(4) NOT NULL,
  `rank_10` tinyint(4) NOT NULL,
  `champion_sum` tinyint(4) DEFAULT NULL,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY `uk_period` (`period_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `water_control` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `game_id` int(11) NOT NULL,
  `bet_type` varchar(32) NOT NULL,
  `win_rate_pct` decimal(5,2) DEFAULT 50.00,
  `enabled` tinyint(4) DEFAULT 0,
  `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY `uk_game_type` (`game_id`,`bet_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `bet_limits` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `game_id` int(11) NOT NULL,
  `bet_type` varchar(32) NOT NULL,
  `min_amount` decimal(14,2) DEFAULT 10.00,
  `max_amount` decimal(14,2) DEFAULT 100000.00,
  `max_per_period` decimal(14,2) DEFAULT 500000.00,
  UNIQUE KEY `uk_game_type` (`game_id`,`bet_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `i18n` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `lang` varchar(5) NOT NULL,
  `key_name` varchar(128) NOT NULL,
  `value` text NOT NULL,
  UNIQUE KEY `uk_lang_key` (`lang`,`key_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `email_verifications` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `email` varchar(128) NOT NULL,
  `code` varchar(8) NOT NULL,
  `expires_at` datetime NOT NULL,
  `used` tinyint(4) DEFAULT 0,
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  KEY `idx_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `agent_commissions` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `agent_id` int(11) NOT NULL,
  `from_user_id` int(11) NOT NULL,
  `period_id` int(11) DEFAULT NULL,
  `bet_amount` decimal(14,2) NOT NULL,
  `commission` decimal(14,2) NOT NULL,
  `type` enum('bet','rebate') DEFAULT 'bet',
  `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
  KEY `idx_agent` (`agent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ========== 初始化PK10游戏 ==========
INSERT INTO `games` (`name`, `code`, `type`, `status`, `stream_url`, `thumbnail`) VALUES
('PK10 Speed Racing', 'pk10', 'pk10', 1, '', '/Static/pk10/pk10-thumb.jpg')
ON DUPLICATE KEY UPDATE `name`=VALUES(`name`);

-- ========== PK10默认赔率 ==========
INSERT INTO `game_odds` (`game_id`, `type`, `target`, `odds`)
SELECT g.id, 'rank', n.target, 9.80
FROM `games` g, (
  SELECT 'rank1_1' AS target UNION SELECT 'rank1_2' UNION SELECT 'rank1_3' UNION SELECT 'rank1_4' UNION SELECT 'rank1_5'
  UNION SELECT 'rank1_6' UNION SELECT 'rank1_7' UNION SELECT 'rank1_8' UNION SELECT 'rank1_9' UNION SELECT 'rank1_10'
  UNION SELECT 'rank2_1' UNION SELECT 'rank2_2' UNION SELECT 'rank2_3' UNION SELECT 'rank2_4' UNION SELECT 'rank2_5'
  UNION SELECT 'rank2_6' UNION SELECT 'rank2_7' UNION SELECT 'rank2_8' UNION SELECT 'rank2_9' UNION SELECT 'rank2_10'
  UNION SELECT 'rank3_1' UNION SELECT 'rank3_2' UNION SELECT 'rank3_3' UNION SELECT 'rank3_4' UNION SELECT 'rank3_5'
  UNION SELECT 'rank3_6' UNION SELECT 'rank3_7' UNION SELECT 'rank3_8' UNION SELECT 'rank3_9' UNION SELECT 'rank3_10'
  UNION SELECT 'rank4_1' UNION SELECT 'rank4_2' UNION SELECT 'rank4_3' UNION SELECT 'rank4_4' UNION SELECT 'rank4_5'
  UNION SELECT 'rank4_6' UNION SELECT 'rank4_7' UNION SELECT 'rank4_8' UNION SELECT 'rank4_9' UNION SELECT 'rank4_10'
  UNION SELECT 'rank5_1' UNION SELECT 'rank5_2' UNION SELECT 'rank5_3' UNION SELECT 'rank5_4' UNION SELECT 'rank5_5'
  UNION SELECT 'rank5_6' UNION SELECT 'rank5_7' UNION SELECT 'rank5_8' UNION SELECT 'rank5_9' UNION SELECT 'rank5_10'
  UNION SELECT 'rank6_1' UNION SELECT 'rank6_2' UNION SELECT 'rank6_3' UNION SELECT 'rank6_4' UNION SELECT 'rank6_5'
  UNION SELECT 'rank6_6' UNION SELECT 'rank6_7' UNION SELECT 'rank6_8' UNION SELECT 'rank6_9' UNION SELECT 'rank6_10'
  UNION SELECT 'rank7_1' UNION SELECT 'rank7_2' UNION SELECT 'rank7_3' UNION SELECT 'rank7_4' UNION SELECT 'rank7_5'
  UNION SELECT 'rank7_6' UNION SELECT 'rank7_7' UNION SELECT 'rank7_8' UNION SELECT 'rank7_9' UNION SELECT 'rank7_10'
  UNION SELECT 'rank8_1' UNION SELECT 'rank8_2' UNION SELECT 'rank8_3' UNION SELECT 'rank8_4' UNION SELECT 'rank8_5'
  UNION SELECT 'rank8_6' UNION SELECT 'rank8_7' UNION SELECT 'rank8_8' UNION SELECT 'rank8_9' UNION SELECT 'rank8_10'
  UNION SELECT 'rank9_1' UNION SELECT 'rank9_2' UNION SELECT 'rank9_3' UNION SELECT 'rank9_4' UNION SELECT 'rank9_5'
  UNION SELECT 'rank9_6' UNION SELECT 'rank9_7' UNION SELECT 'rank9_8' UNION SELECT 'rank9_9' UNION SELECT 'rank9_10'
  UNION SELECT 'rank10_1' UNION SELECT 'rank10_2' UNION SELECT 'rank10_3' UNION SELECT 'rank10_4' UNION SELECT 'rank10_5'
  UNION SELECT 'rank10_6' UNION SELECT 'rank10_7' UNION SELECT 'rank10_8' UNION SELECT 'rank10_9' UNION SELECT 'rank10_10'
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `odds`=VALUES(`odds`);

INSERT INTO `game_odds` (`game_id`, `type`, `target`, `odds`)
SELECT g.id, 'bs', n.target, 1.95
FROM `games` g, (
  SELECT 'rank1_big' AS target UNION SELECT 'rank1_small'
  UNION SELECT 'rank2_big' UNION SELECT 'rank2_small'
  UNION SELECT 'rank3_big' UNION SELECT 'rank3_small'
  UNION SELECT 'rank4_big' UNION SELECT 'rank4_small'
  UNION SELECT 'rank5_big' UNION SELECT 'rank5_small'
  UNION SELECT 'rank6_big' UNION SELECT 'rank6_small'
  UNION SELECT 'rank7_big' UNION SELECT 'rank7_small'
  UNION SELECT 'rank8_big' UNION SELECT 'rank8_small'
  UNION SELECT 'rank9_big' UNION SELECT 'rank9_small'
  UNION SELECT 'rank10_big' UNION SELECT 'rank10_small'
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `odds`=VALUES(`odds`);

INSERT INTO `game_odds` (`game_id`, `type`, `target`, `odds`)
SELECT g.id, 'oe', n.target, 1.95
FROM `games` g, (
  SELECT 'rank1_odd' AS target UNION SELECT 'rank1_even'
  UNION SELECT 'rank2_odd' UNION SELECT 'rank2_even'
  UNION SELECT 'rank3_odd' UNION SELECT 'rank3_even'
  UNION SELECT 'rank4_odd' UNION SELECT 'rank4_even'
  UNION SELECT 'rank5_odd' UNION SELECT 'rank5_even'
  UNION SELECT 'rank6_odd' UNION SELECT 'rank6_even'
  UNION SELECT 'rank7_odd' UNION SELECT 'rank7_even'
  UNION SELECT 'rank8_odd' UNION SELECT 'rank8_even'
  UNION SELECT 'rank9_odd' UNION SELECT 'rank9_even'
  UNION SELECT 'rank10_odd' UNION SELECT 'rank10_even'
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `odds`=VALUES(`odds`);

INSERT INTO `game_odds` (`game_id`, `type`, `target`, `odds`)
SELECT g.id, 'dt', n.target, 1.95
FROM `games` g, (
  SELECT 'dt1_dragon' AS target UNION SELECT 'dt1_tiger'
  UNION SELECT 'dt2_dragon' UNION SELECT 'dt2_tiger'
  UNION SELECT 'dt3_dragon' UNION SELECT 'dt3_tiger'
  UNION SELECT 'dt4_dragon' UNION SELECT 'dt4_tiger'
  UNION SELECT 'dt5_dragon' UNION SELECT 'dt5_tiger'
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `odds`=VALUES(`odds`);

INSERT INTO `game_odds` (`game_id`, `type`, `target`, `odds`)
SELECT g.id, 'sum', n.target, n.odds
FROM `games` g, (
  SELECT 'sum_3' AS target, 140.00 AS odds
  UNION SELECT 'sum_4', 70.00 UNION SELECT 'sum_5', 35.00 UNION SELECT 'sum_6', 23.00
  UNION SELECT 'sum_7', 17.50 UNION SELECT 'sum_8', 14.00 UNION SELECT 'sum_9', 11.60
  UNION SELECT 'sum_10', 10.00 UNION SELECT 'sum_11', 8.80
  UNION SELECT 'sum_12', 8.80 UNION SELECT 'sum_13', 10.00
  UNION SELECT 'sum_14', 11.60 UNION SELECT 'sum_15', 14.00 UNION SELECT 'sum_16', 17.50
  UNION SELECT 'sum_17', 23.00 UNION SELECT 'sum_18', 35.00 UNION SELECT 'sum_19', 70.00
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `odds`=VALUES(`odds`);

INSERT INTO `game_odds` (`game_id`, `type`, `target`, `odds`)
SELECT g.id, 'sum_bs', n.target, 1.95
FROM `games` g, (
  SELECT 'sum_big' AS target UNION SELECT 'sum_small'
  UNION SELECT 'sum_odd' UNION SELECT 'sum_even'
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `odds`=VALUES(`odds`);

-- ========== 默认放水配置 ==========
INSERT INTO `water_control` (`game_id`, `bet_type`, `win_rate_pct`, `enabled`)
SELECT g.id, n.bet_type, 48.00, 0
FROM `games` g, (
  SELECT 'bs' AS bet_type UNION SELECT 'oe' UNION SELECT 'dt' UNION SELECT 'sum_bs'
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `win_rate_pct`=VALUES(`win_rate_pct`);

-- ========== 默认投注限额 ==========
INSERT INTO `bet_limits` (`game_id`, `bet_type`, `min_amount`, `max_amount`, `max_per_period`)
SELECT g.id, n.bet_type, 10.00, 100000.00, 500000.00
FROM `games` g, (
  SELECT 'rank' AS bet_type UNION SELECT 'bs' UNION SELECT 'oe'
  UNION SELECT 'dt' UNION SELECT 'sum' UNION SELECT 'sum_bs'
) n WHERE g.code='pk10'
ON DUPLICATE KEY UPDATE `min_amount`=VALUES(`min_amount`);

-- ========== 默认系统设置 ==========
INSERT INTO `system_settings` (`setting_key`, `setting_value`) VALUES
('site_title', 'PK10 Speed Racing'),
('site_description', 'PK10 Speed Racing - Online Betting Platform'),
('site_keywords', 'pk10, speed racing, betting'),
('site_logo', '/Static/images/logo.png'),
('site_favicon', '/Static/css/favicon.ico'),
('site_copyright', '© 2025 PK10 Racing. All rights reserved.')
ON DUPLICATE KEY UPDATE `setting_value`=VALUES(`setting_value`);
