-- R0Installer 云端配置数据库
-- 使用 MySQL 5.7+ / MariaDB 10.2+

CREATE DATABASE IF NOT EXISTS `r0_installer` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `r0_installer`;

-- 项目表
CREATE TABLE IF NOT EXISTS `projects` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `project_id` VARCHAR(50) NOT NULL UNIQUE,
    `display_name` VARCHAR(100) NOT NULL,
    `description` TEXT,
    `icon_url` VARCHAR(500) DEFAULT '',
    `enabled` TINYINT(1) DEFAULT 1,
    `config_json` JSON NOT NULL,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 安装器全局配置表
CREATE TABLE IF NOT EXISTS `installer_config` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `config_key` VARCHAR(50) NOT NULL UNIQUE DEFAULT 'global',
    `installer_version` VARCHAR(20) DEFAULT '2.0',
    `min_client_version` VARCHAR(20) DEFAULT '1.0.0.0',
    `filename_rules` JSON NOT NULL,
    `command_rules` JSON NOT NULL,
    `default_project` VARCHAR(50) DEFAULT 'r0_arena',
    `default_mode` VARCHAR(20) DEFAULT 'full',
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 项目版本表
CREATE TABLE IF NOT EXISTS `project_versions` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `project_id` VARCHAR(50) NOT NULL,
    `version` VARCHAR(50) NOT NULL,
    `download_url` VARCHAR(500) NOT NULL,
    `file_size` BIGINT DEFAULT 0,
    `changelog` TEXT,
    `threads` INT DEFAULT 8,
    `extra_downloads` JSON DEFAULT NULL,
    `runtime_urls` JSON DEFAULT NULL,
    `is_latest` TINYINT(1) DEFAULT 0,
    `silent_update` TINYINT(1) DEFAULT 0,   -- 1=更新到该版本时静默更新（不弹界面），0=显示更新界面
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_project_version` (`project_id`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 已有数据库升级：为旧的 project_versions 表补充 silent_update 列（首次部署可忽略本行；
-- MySQL 8 不支持 ADD COLUMN IF NOT EXISTS，若列已存在会报错，可直接跳过执行）。
-- ALTER TABLE `project_versions` ADD COLUMN `silent_update` TINYINT(1) DEFAULT 0 AFTER `is_latest`;

-- 增量更新包表
CREATE TABLE IF NOT EXISTS `project_patches` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `project_id` VARCHAR(50) NOT NULL,
    `from_version` VARCHAR(50) NOT NULL,
    `to_version` VARCHAR(50) NOT NULL,
    `download_url` VARCHAR(500) NOT NULL,
    `file_size` BIGINT DEFAULT 0,
    `changelog` TEXT,
    `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_patch` (`project_id`, `from_version`, `to_version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ========== 初始数据 ==========

-- 安装器全局配置
INSERT INTO `installer_config` (`config_key`, `installer_version`, `min_client_version`, `filename_rules`, `command_rules`, `default_project`, `default_mode`)
VALUES ('global', '2.0', '1.0.0.0',
'[{"pattern":"R0ClientInstaller","match_type":"starts_with","case_sensitive":false,"project_id":"r0_arena","install_mode":"client_only","description":"仅安装R0对战平台客户端"},{"pattern":"R0AcInstaller","match_type":"starts_with","case_sensitive":false,"project_id":"r0_arena","install_mode":"ac_only","description":"仅安装R0对战平台环境和反作弊"},{"pattern":"R0Installer","match_type":"starts_with","case_sensitive":false,"project_id":"r0_arena","install_mode":"full","description":"完整安装R0对战平台"}]',
'[{"command":"update","project_id":"r0_arena","update_target":"update","description":"R0对战平台主程序更新"},{"command":"update_ac","project_id":"r0_arena","update_target":"update_ac","description":"R0对战平台反作弊更新"}]',
'r0_arena', 'full');

-- R0对战平台项目
INSERT INTO `projects` (`project_id`, `display_name`, `description`, `enabled`, `config_json`)
VALUES ('r0_arena', 'R0对战平台', 'R0对战平台客户端', 1,
'{
    "install": {
        "window_title": "R0对战平台安装程序",
        "steps": [
            {"step_id": "runtime", "display_name": "环境检查", "type": "runtime_check", "enabled": true},
            {"step_id": "anticheat", "display_name": "反作弊安装", "type": "download_and_run", "enabled": true, "download_url_field": "download_url_ac_cdn", "temp_filename_template": "r0_guard_v{version}.exe", "installer_args": "", "wait_for_exit": true},
            {"step_id": "main", "display_name": "主程序安装", "type": "download_and_run", "enabled": true, "download_url_field": "download_url_cdn", "temp_filename_template": "r0_arena_v{version}.exe", "installer_args": "", "wait_for_exit": false}
        ]
    },
    "update": {
        "window_title": "R0对战平台更新程序",
        "command": "update",
        "check_api": "/v3/update/check?project=r0_arena&v={0}",
        "processes_to_close": ["r0_arena", "R0对战平台"],
        "launch_after_update": "R0对战平台.exe",
        "temp_filename_template": "r0_arena_v{version}.exe",
        "hold_file": null,
        "supports_incremental": true
    },
    "update_ac": {
        "window_title": "R0反作弊更新程序",
        "command": "update_ac",
        "check_api": "/v3/update/check?project=r0_guard&v={0}",
        "processes_to_close": ["R0Guard"],
        "launch_after_update": "R0Guard.exe",
        "temp_filename_template": "r0_guard_v{version}.exe",
        "hold_file": "ac.hold_install",
        "supports_incremental": true
    },
    "runtime_requirements": [
        {
            "runtime_id": "vc_redist",
            "display_name": "VC++运行库",
            "enabled": true,
            "always_install": true,
            "detection": {"type": "registry", "keys": ["SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\14.0\\\\VC\\\\Runtimes\\\\x86","SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\14.0\\\\VC\\\\Runtimes\\\\x64","SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\15.0\\\\VC\\\\Runtimes\\\\x86","SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\15.0\\\\VC\\\\Runtimes\\\\x64","SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\16.0\\\\VC\\\\Runtimes\\\\x86","SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\16.0\\\\VC\\\\Runtimes\\\\x64","SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\17.0\\\\VC\\\\Runtimes\\\\x86","SOFTWARE\\\\Microsoft\\\\VisualStudio\\\\17.0\\\\VC\\\\Runtimes\\\\x64"], "value_name": "Installed", "expected_value": 1},
            "download_url_x86": "https://cdn.r0csgo.com/runtime/VC_redist.x86.exe",
            "download_url_x64": "https://cdn.r0csgo.com/runtime/VC_redist.x64.exe",
            "temp_filename_template": "VC_redist.{arch}.exe",
            "install_args": "/install /passive /norestart"
        },
        {
            "runtime_id": "dotnet_desktop",
            "display_name": ".NET运行库",
            "enabled": false,
            "always_install": false,
            "detection": {"type": "dotnet_runtime", "runtime_name": "Microsoft.WindowsDesktop.App", "version": "8.0.21"},
            "download_url_x86": "https://cdn.r0csgo.com/runtime/windowsdesktop-runtime-8.0.21-win-x86.exe",
            "download_url_x64": "https://cdn.r0csgo.com/runtime/windowsdesktop-runtime-8.0.21-win-x64.exe",
            "temp_filename_template": "windowsdesktop-runtime-{version}-win-{arch}.exe",
            "install_args": "/install /passive /norestart"
        },
        {
            "runtime_id": "aspnet_core",
            "display_name": "ASP.NET Core运行库",
            "enabled": false,
            "always_install": false,
            "detection": {"type": "dotnet_runtime", "runtime_name": "Microsoft.AspNetCore.App", "version": "8.0.21"},
            "download_url_x86": "https://cdn.r0csgo.com/runtime/aspnetcore-runtime-8.0.21-win-x86.exe",
            "download_url_x64": "https://cdn.r0csgo.com/runtime/aspnetcore-runtime-8.0.21-win-x64.exe",
            "temp_filename_template": "aspnetcore-runtime-{version}-win-{arch}.exe",
            "install_args": "/install /passive /norestart"
        }
    ],
    "download_config": {
        "chunk_size": 10485760,
        "max_threads": 8,
        "retry_count": 3,
        "timeout_ms": 30000,
        "user_agent": "r0_installer"
    }
}');

-- 示例版本
INSERT INTO `project_versions` (`project_id`, `version`, `download_url`, `file_size`, `changelog`, `threads`, `extra_downloads`, `runtime_urls`, `is_latest`)
VALUES ('r0_arena', '3.0.5', 'https://cdn.r0csgo.com/releases/r0_arena_v3.0.5.exe', 0, '最新版本', 8,
'{"download_url_ac_cdn": "https://cdn.r0csgo.com/releases/r0_guard_v2.1.0.exe"}',
'{"VC_REDISTRIB_X86_URL":"https://cdn.r0csgo.com/runtime/VC_redist.x86.exe","VC_REDISTRIB_X64_URL":"https://cdn.r0csgo.com/runtime/VC_redist.x64.exe"}',
1);
