<?php
/**
 * HuSNS - 一款免费开源的社交平台
 * 
 * @author  HYR
 * @QQ      281900864
 * @website https://huyourui.com
 * @license MIT
 * @声明    严禁用于违法违规用途
 */
class InstallController
{
    private $view;
    private $errors = [];
    private $step = 1;

    public function __construct()
    {
        $this->view = new View();
        $this->view->setLayout(false);
    }

    public function index()
    {
        $this->step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
        
        switch ($this->step) {
            case 1:
                $this->step1();
                break;
            case 2:
                $this->step2();
                break;
            case 3:
                $this->step3();
                break;
            case 4:
                $this->step4();
                break;
            default:
                $this->step1();
        }
    }

    private function step1()
    {
        if (Helper::isPost()) {
            $requirements = $this->checkRequirements();
            if ($this->checkAllPassed($requirements)) {
                Helper::redirect('install.php?step=2');
            }
        }
        
        $requirements = $this->checkRequirements();
        $this->view->assign('step', 1);
        $this->view->assign('requirements', $requirements);
        $this->view->assign('passed', $this->checkAllPassed($requirements));
        echo $this->view->render('install/step1');
    }

    private function step2()
    {
        if (Helper::isPost()) {
            $host = trim(Helper::post('db_host', 'localhost'));
            $port = (int)Helper::post('db_port', 3306);
            $name = trim(Helper::post('db_name', 'husns'));
            $user = trim(Helper::post('db_user', 'root'));
            $pass = Helper::post('db_pass', '');
            $prefix = trim(Helper::post('db_prefix', 'husns_'));

            if (empty($host) || empty($name) || empty($user)) {
                $this->errors[] = '请填写完整的数据库信息';
            }

            if (!Database::testConnection($host, $port, $name, $user, $pass)) {
                if (!Database::createDatabase($host, $port, $user, $pass, $name)) {
                    $this->errors[] = '数据库连接失败，请检查配置或手动创建数据库';
                }
            }

            if (empty($this->errors)) {
                $_SESSION['install_config'] = [
                    'db_host' => $host,
                    'db_port' => $port,
                    'db_name' => $name,
                    'db_user' => $user,
                    'db_pass' => $pass,
                    'db_prefix' => $prefix
                ];
                Helper::redirect('install.php?step=3');
            }
        }

        $this->view->assign('step', 2);
        $this->view->assign('errors', $this->errors);
        echo $this->view->render('install/step2');
    }

    private function step3()
    {
        if (!isset($_SESSION['install_config'])) {
            Helper::redirect('install.php?step=2');
        }

        if (Helper::isPost()) {
            try {
                $username = trim(Helper::post('username', ''));
                $password = Helper::post('password', '');
                $confirmPassword = Helper::post('confirm_password', '');
                $email = trim(Helper::post('email', ''));

                if (!Security::validateUsername($username)) {
                    $this->errors[] = '用户名格式不正确（3-20位字母开头，可包含数字和下划线）';
                }

                if (strlen($password) < 6) {
                    $this->errors[] = '密码长度至少6位';
                }

                if ($password !== $confirmPassword) {
                    $this->errors[] = '两次密码输入不一致';
                }

                if (!Security::validateEmail($email)) {
                    $this->errors[] = '邮箱格式不正确';
                }

                if (empty($this->errors)) {
                    $config = $_SESSION['install_config'];
                    $this->writeConfig($config);
                    
                    require_once ROOT_PATH . 'config.php';
                    
                    $this->createTables($config);
                    $this->createAdmin($username, $password, $email, $config);
                    $this->createLockFile();
                    unset($_SESSION['install_config']);
                    Helper::redirect('install.php?step=4');
                }
            } catch (Exception $e) {
                $this->errors[] = '安装错误：' . $e->getMessage();
            }
        }

        $this->view->assign('step', 3);
        $this->view->assign('errors', $this->errors);
        echo $this->view->render('install/step3');
    }

    private function step4()
    {
        $this->view->assign('step', 4);
        echo $this->view->render('install/step4');
    }

    private function checkRequirements()
    {
        if (!is_dir(ROOT_PATH . 'uploads')) {
            mkdir(ROOT_PATH . 'uploads', 0755, true);
        }
        
        $this->tryFixPermission(ROOT_PATH . 'uploads');
        $this->tryFixPermission(ROOT_PATH);
        
        return [
            'php_version' => [
                'name' => 'PHP版本',
                'required' => '7.4+',
                'current' => PHP_VERSION,
                'passed' => version_compare(PHP_VERSION, '7.4.0', '>=')
            ],
            'pdo_mysql' => [
                'name' => 'PDO MySQL扩展',
                'required' => '必须',
                'current' => extension_loaded('pdo_mysql') ? '已安装' : '未安装',
                'passed' => extension_loaded('pdo_mysql')
            ],
            'gd' => [
                'name' => 'GD库',
                'required' => '必须',
                'current' => extension_loaded('gd') ? '已安装' : '未安装',
                'passed' => extension_loaded('gd')
            ],
            'mbstring' => [
                'name' => 'MBString扩展',
                'required' => '必须',
                'current' => extension_loaded('mbstring') ? '已安装' : '未安装',
                'passed' => extension_loaded('mbstring')
            ],
            'json' => [
                'name' => 'JSON扩展',
                'required' => '必须',
                'current' => extension_loaded('json') ? '已安装' : '未安装',
                'passed' => extension_loaded('json')
            ],
            'upload_dir' => [
                'name' => '上传目录可写',
                'required' => '必须',
                'current' => is_writable(ROOT_PATH . 'uploads') ? '可写' : '不可写',
                'passed' => is_writable(ROOT_PATH . 'uploads')
            ],
            'config_dir' => [
                'name' => '配置文件可写',
                'required' => '必须',
                'current' => is_writable(ROOT_PATH) ? '可写' : '不可写',
                'passed' => is_writable(ROOT_PATH)
            ]
        ];
    }
    
    private function tryFixPermission($path)
    {
        if (!is_writable($path)) {
            @chmod($path, 0755);
        }
        if (!is_writable($path)) {
            @chmod($path, 0777);
        }
    }

    private function checkAllPassed($requirements)
    {
        foreach ($requirements as $item) {
            if (!$item['passed']) {
                return false;
            }
        }
        return true;
    }

    private function writeConfig($config)
    {
        $configFile = ROOT_PATH . 'config.php';
        
        if (file_exists($configFile)) {
            $this->tryFixPermission($configFile);
            if (!is_writable($configFile)) {
                throw new Exception('配置文件不可写，请检查权限');
            }
        }
        
        if (!file_exists($configFile)) {
            touch($configFile);
            $this->tryFixPermission($configFile);
        }
        
        $configContent = <<<'PHP'
<?php
if (!defined('ROOT_PATH')) {
    define('ROOT_PATH', str_replace('\\', '/', dirname(__FILE__)) . '/');
}

define('DB_HOST', '{db_host}');
define('DB_PORT', {db_port});
define('DB_NAME', '{db_name}');
define('DB_USER', '{db_user}');
define('DB_PASS', '{db_pass}');
define('DB_PREFIX', '{db_prefix}');
define('DB_CHARSET', 'utf8mb4');

define('SITE_URL', '{site_url}');
define('SITE_NAME', 'HuSNS');
define('SITE_DEBUG', false);
define('URL_REWRITE', false);

define('UPLOAD_PATH', ROOT_PATH . 'uploads/');
define('UPLOAD_MAX_SIZE', 5 * 1024 * 1024);
define('UPLOAD_ALLOW_TYPES', 'jpg,jpeg,png,gif');

define('SESSION_NAME', 'HUSNS_SESSION');
define('SESSION_LIFETIME', 7200);

define('PASSWORD_SALT', '{salt}');

date_default_timezone_set('Asia/Shanghai');

if (SITE_DEBUG) {
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
} else {
    error_reporting(0);
    ini_set('display_errors', 'Off');
}

session_name(SESSION_NAME);
session_start();
PHP;

        $baseUrl = Helper::getBaseUrl();
        $salt = Security::generateRandomString(32);
        
        $configContent = str_replace(
            ['{db_host}', '{db_port}', '{db_name}', '{db_user}', '{db_pass}', '{db_prefix}', '{site_url}', '{salt}'],
            [$config['db_host'], $config['db_port'], $config['db_name'], $config['db_user'], $config['db_pass'], $config['db_prefix'], $baseUrl, $salt],
            $configContent
        );

        $result = file_put_contents($configFile, $configContent);
        if ($result === false) {
            throw new Exception('配置文件写入失败');
        }
    }

    private function createTables($config)
    {
        $dsn = "mysql:host={$config['db_host']};port={$config['db_port']};dbname={$config['db_name']};charset=utf8mb4";
        $pdo = new PDO($dsn, $config['db_user'], $config['db_pass'], [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ]);

        $prefix = $config['db_prefix'];

        $dropTables = [
            "DROP TABLE IF EXISTS `{$prefix}admin_role_users`",
            "DROP TABLE IF EXISTS `{$prefix}admin_roles`",
            "DROP TABLE IF EXISTS `{$prefix}filter_words`",
            "DROP TABLE IF EXISTS `{$prefix}circle_posts`",
            "DROP TABLE IF EXISTS `{$prefix}circle_members`",
            "DROP TABLE IF EXISTS `{$prefix}circles`",
            "DROP TABLE IF EXISTS `{$prefix}reports`",
            "DROP TABLE IF EXISTS `{$prefix}comment_likes`",
            "DROP TABLE IF EXISTS `{$prefix}favorites`",
            "DROP TABLE IF EXISTS `{$prefix}invite_codes`",
            "DROP TABLE IF EXISTS `{$prefix}private_messages`",
            "DROP TABLE IF EXISTS `{$prefix}verification_codes`",
            "DROP TABLE IF EXISTS `{$prefix}queue_jobs`",
            "DROP TABLE IF EXISTS `{$prefix}api_tokens`",
            "DROP TABLE IF EXISTS `{$prefix}system_logs`",
            "DROP TABLE IF EXISTS `{$prefix}point_logs`",
            "DROP TABLE IF EXISTS `{$prefix}point_rules`",
            "DROP TABLE IF EXISTS `{$prefix}links`",
            "DROP TABLE IF EXISTS `{$prefix}announcements`",
            "DROP TABLE IF EXISTS `{$prefix}topics`",
            "DROP TABLE IF EXISTS `{$prefix}notifications`",
            "DROP TABLE IF EXISTS `{$prefix}follows`",
            "DROP TABLE IF EXISTS `{$prefix}likes`",
            "DROP TABLE IF EXISTS `{$prefix}comments`",
            "DROP TABLE IF EXISTS `{$prefix}posts`",
            "DROP TABLE IF EXISTS `{$prefix}users`",
            "DROP TABLE IF EXISTS `{$prefix}settings`",
            "DROP TABLE IF EXISTS `{$prefix}plugins`",
            "DROP TABLE IF EXISTS `{$prefix}version`",
        ];

        foreach ($dropTables as $sql) {
            $pdo->exec($sql);
        }

        $tables = [
            "CREATE TABLE IF NOT EXISTS `{$prefix}users` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `username` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
              `password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码',
              `email` varchar(100) NOT NULL DEFAULT '' COMMENT '邮箱',
              `avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '头像',
              `background` varchar(255) NOT NULL DEFAULT '' COMMENT '个人主页背景图片',
              `bio` varchar(255) NOT NULL DEFAULT '' COMMENT '个人简介',
              `remember_token` varchar(64) NOT NULL DEFAULT '' COMMENT '保持登录令牌',
              `is_admin` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否管理员',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1正常',
              `ban_type` tinyint(1) NOT NULL DEFAULT 0 COMMENT '限制类型：0正常 1禁言 2封禁',
              `ban_until` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '限制到期时间，0表示永久',
              `ban_reason` varchar(255) NOT NULL DEFAULT '' COMMENT '限制原因',
              `points` int(11) NOT NULL DEFAULT 0 COMMENT '积分',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '注册时间',
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
              `last_activity` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '最后活跃时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `username` (`username`),
              UNIQUE KEY `email` (`email`),
              KEY `status` (`status`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}posts` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `is_anonymous` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否匿名发布：0否 1是',
              `content` text NOT NULL COMMENT '内容',
              `images` varchar(2000) NOT NULL DEFAULT '' COMMENT '图片JSON',
              `attachments` text COMMENT '附件JSON',
              `videos` text COMMENT '视频JSON',
              `repost_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '转发的原动态ID，0为原创',
              `repost_user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '被转发动态的用户ID',
              `likes` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '点赞数',
              `comments` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '评论数',
              `reposts` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '转发数',
              `views` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '阅读量',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0删除 1正常',
              `is_pinned` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否置顶：0否 1是',
              `is_featured` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否精华：0否 1是',
              `edit_count` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '编辑次数',
              `edited_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '最后编辑时间',
              `ip` varchar(50) NOT NULL DEFAULT '' COMMENT '发布IP',
              `location_name` varchar(255) NOT NULL DEFAULT '' COMMENT '位置名称',
              `location_lat` decimal(10,7) DEFAULT NULL COMMENT '纬度',
              `location_lng` decimal(10,7) DEFAULT NULL COMMENT '经度',
              `circle_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '圈子ID',
              `circle_name` varchar(100) NOT NULL DEFAULT '' COMMENT '圈子名称',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
              PRIMARY KEY (`id`),
              KEY `user_id` (`user_id`),
              KEY `status` (`status`),
              KEY `created_at` (`created_at`),
              KEY `is_pinned` (`is_pinned`),
              KEY `is_featured` (`is_featured`),
              KEY `repost_id` (`repost_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='动态表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}comments` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `post_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '动态ID',
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `content` varchar(500) NOT NULL DEFAULT '' COMMENT '评论内容',
              `parent_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '父评论ID，0为一级评论',
              `reply_to_user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '回复的用户ID',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态',
              `likes` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '点赞数',
              `ip` varchar(50) NOT NULL DEFAULT '' COMMENT '评论IP',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              KEY `post_id` (`post_id`),
              KEY `user_id` (`user_id`),
              KEY `parent_id` (`parent_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='评论表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}likes` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `post_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '动态ID',
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `post_user` (`post_id`, `user_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='点赞表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}follows` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `follow_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '关注用户ID',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `user_follow` (`user_id`, `follow_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='关注表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}plugins` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `name` varchar(50) NOT NULL DEFAULT '' COMMENT '插件名',
              `status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '状态：0禁用 1启用',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `name` (`name`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='插件表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}settings` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `key` varchar(50) NOT NULL DEFAULT '' COMMENT '配置键',
              `value` text COMMENT '配置值',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `key` (`key`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统配置表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}notifications` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '接收用户ID',
              `type` varchar(30) NOT NULL DEFAULT '' COMMENT '通知类型',
              `title` varchar(255) NOT NULL DEFAULT '' COMMENT '通知标题',
              `content` text COMMENT '通知内容',
              `data` text COMMENT '扩展数据JSON',
              `sender_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '发送者ID',
              `target_type` varchar(30) NOT NULL DEFAULT '' COMMENT '目标类型',
              `target_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '目标ID',
              `is_read` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否已读：0未读 1已读',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              KEY `user_id` (`user_id`),
              KEY `type` (`type`),
              KEY `is_read` (`is_read`),
              KEY `created_at` (`created_at`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消息通知表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}version` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `version` varchar(20) NOT NULL DEFAULT '' COMMENT '版本号',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='版本表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}links` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `name` varchar(100) NOT NULL COMMENT '网站名称',
              `url` varchar(255) NOT NULL COMMENT '网站地址',
              `description` varchar(255) DEFAULT '' COMMENT '网站描述',
              `logo` varchar(255) DEFAULT '' COMMENT '网站LOGO',
              `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1启用',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              KEY `status` (`status`),
              KEY `sort_order` (`sort_order`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='友情链接表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}topics` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `name` varchar(100) NOT NULL COMMENT '话题名称',
              `is_pinned` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否人工置顶：0否 1是',
              `is_blocked` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否屏蔽：0否 1是',
              `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序（人工置顶时有效）',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              UNIQUE KEY `name` (`name`),
              KEY `is_pinned` (`is_pinned`),
              KEY `is_blocked` (`is_blocked`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='话题管理表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}announcements` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `title` varchar(200) NOT NULL DEFAULT '' COMMENT '标题',
              `content` text COMMENT '内容',
              `color` varchar(20) NOT NULL DEFAULT 'blue' COMMENT '颜色：blue/green/yellow/red/purple/cyan',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1启用',
              `sort_order` int(11) NOT NULL DEFAULT 0 COMMENT '排序',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              KEY `status` (`status`),
              KEY `sort_order` (`sort_order`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='公告表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}point_logs` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `action` varchar(50) NOT NULL DEFAULT '' COMMENT '动作标识',
              `points` int(11) NOT NULL DEFAULT 0 COMMENT '积分变动',
              `balance` int(11) NOT NULL DEFAULT 0 COMMENT '变动后余额',
              `related_type` varchar(30) NOT NULL DEFAULT '' COMMENT '关联类型',
              `related_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '关联ID',
              `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              KEY `user_id` (`user_id`),
              KEY `action` (`action`),
              KEY `created_at` (`created_at`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='积分日志表'",
            
            "CREATE TABLE IF NOT EXISTS `{$prefix}point_rules` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `action` varchar(50) NOT NULL DEFAULT '' COMMENT '动作标识',
              `name` varchar(100) NOT NULL DEFAULT '' COMMENT '规则名称',
              `points` int(11) NOT NULL DEFAULT 0 COMMENT '积分变动值',
              `daily_limit` int(11) NOT NULL DEFAULT 0 COMMENT '每日限制次数，0为不限',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1启用',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              UNIQUE KEY `action` (`action`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='积分规则表'",
            
            /**
             * 系统日志表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}system_logs` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `level` varchar(20) NOT NULL DEFAULT 'info' COMMENT '日志级别',
              `message` text NOT NULL COMMENT '日志消息',
              `context` text COMMENT '上下文数据JSON',
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `ip` varchar(50) NOT NULL DEFAULT '' COMMENT 'IP地址',
              `user_agent` varchar(500) NOT NULL DEFAULT '' COMMENT '用户代理',
              `request_uri` varchar(500) NOT NULL DEFAULT '' COMMENT '请求URI',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              KEY `level` (`level`),
              KEY `user_id` (`user_id`),
              KEY `created_at` (`created_at`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统日志表'",
            
            /**
             * API Token表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}api_tokens` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `token` varchar(64) NOT NULL DEFAULT '' COMMENT 'API Token',
              `name` varchar(100) NOT NULL DEFAULT '' COMMENT 'Token名称',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1启用',
              `expires_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '过期时间',
              `last_used_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '最后使用时间',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `token` (`token`),
              KEY `user_id` (`user_id`),
              KEY `status` (`status`),
              KEY `expires_at` (`expires_at`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='API Token表'",
            
            /**
             * 队列任务表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}queue_jobs` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `queue` varchar(50) NOT NULL DEFAULT 'default' COMMENT '队列名称',
              `job_type` varchar(50) NOT NULL DEFAULT '' COMMENT '任务类型',
              `payload` text COMMENT '任务数据JSON',
              `status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '状态：0待处理 1处理中 2已完成 3失败',
              `priority` int(11) NOT NULL DEFAULT 0 COMMENT '优先级',
              `retries` int(11) NOT NULL DEFAULT 0 COMMENT '重试次数',
              `max_retries` int(11) NOT NULL DEFAULT 3 COMMENT '最大重试次数',
              `last_error` text COMMENT '最后错误信息',
              `available_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '可用时间',
              `started_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '开始时间',
              `reserved_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '保留时间',
              `finished_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '完成时间',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              KEY `queue` (`queue`),
              KEY `status` (`status`),
              KEY `priority` (`priority`),
              KEY `available_at` (`available_at`),
              KEY `created_at` (`created_at`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='队列任务表'",
            
            /**
             * 邮箱验证码表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}verification_codes` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `email` varchar(100) NOT NULL COMMENT '邮箱',
              `code` varchar(10) NOT NULL COMMENT '验证码',
              `purpose` varchar(50) NOT NULL COMMENT '用途',
              `expire_time` datetime NOT NULL COMMENT '过期时间',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              KEY `email` (`email`),
              KEY `purpose` (`purpose`),
              KEY `expire_time` (`expire_time`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='邮箱验证码表'",
            
            /**
             * 私信表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}private_messages` (
              `id` int(11) NOT NULL AUTO_INCREMENT,
              `sender_id` int(11) NOT NULL DEFAULT 0 COMMENT '发送者ID',
              `receiver_id` int(11) NOT NULL DEFAULT 0 COMMENT '接收者ID',
              `content` text NOT NULL COMMENT '私信内容',
              `is_read` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否已读：0未读 1已读',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0删除 1正常',
              `created_at` int(11) NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              KEY `idx_sender` (`sender_id`),
              KEY `idx_receiver` (`receiver_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='私信表'",
            
            /**
             * 邀请码表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}invite_codes` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `code` varchar(32) NOT NULL COMMENT '邀请码',
              `status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '状态：0未使用 1已使用',
              `used_by` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '使用者用户ID',
              `used_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '使用时间',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `code` (`code`),
              KEY `status` (`status`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='邀请码表'",
            
            /**
             * 收藏表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}favorites` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `post_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '动态ID',
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `post_user` (`post_id`, `user_id`),
              KEY `user_id` (`user_id`),
              KEY `created_at` (`created_at`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收藏表'",
            
            /**
             * 评论点赞表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}comment_likes` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `comment_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '评论ID',
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `comment_user` (`comment_id`, `user_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='评论点赞表'",
            
            /**
             * 举报表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}reports` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '举报用户ID',
              `post_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '被举报的帖子ID',
              `reason` varchar(50) NOT NULL DEFAULT '' COMMENT '举报原因',
              `detail` text COMMENT '详细说明',
              `status` tinyint(1) NOT NULL DEFAULT 0 COMMENT '状态：0待处理 1已处理 2已驳回',
              `handled_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '处理时间',
              `handled_by` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '处理人ID',
              `handle_result` varchar(255) NOT NULL DEFAULT '' COMMENT '处理结果说明',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              KEY `user_id` (`user_id`),
              KEY `post_id` (`post_id`),
              KEY `status` (`status`),
              KEY `created_at` (`created_at`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='举报表'",
            
            /**
             * 圈子表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}circles` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `name` varchar(100) NOT NULL DEFAULT '' COMMENT '圈子名称',
              `description` text COMMENT '圈子描述',
              `avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '圈子头像',
              `cover` varchar(255) NOT NULL DEFAULT '' COMMENT '圈子封面',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1正常',
              `is_private` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否私密：0公开 1私密',
              `is_verified` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否认证',
              `verify_type` tinyint(1) NOT NULL DEFAULT 0 COMMENT '认证类型 0-无 1-官方认证 2-企业认证 3-个人认证',
              `verify_info` varchar(200) NOT NULL DEFAULT '' COMMENT '认证信息/说明',
              `verify_time` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '认证时间',
              `verify_admin` int(11) NOT NULL DEFAULT 0 COMMENT '认证管理员ID',
              `post_count` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '帖子数',
              `member_count` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '成员数',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新时间',
              PRIMARY KEY (`id`),
              KEY `status` (`status`),
              KEY `is_private` (`is_private`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='圈子表'",
            
            /**
             * 圈子成员表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}circle_members` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `circle_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '圈子ID',
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `role` tinyint(1) NOT NULL DEFAULT 0 COMMENT '角色：0普通成员 1管理员 2创建者',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0退出 1正常',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '加入时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `circle_user` (`circle_id`, `user_id`),
              KEY `circle_id` (`circle_id`),
              KEY `user_id` (`user_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='圈子成员表'",
            
            /**
             * 圈子帖子关联表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}circle_posts` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `circle_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '圈子ID',
              `post_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '帖子ID',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `circle_post` (`circle_id`, `post_id`),
              KEY `circle_id` (`circle_id`),
              KEY `post_id` (`post_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='圈子帖子关联表'",
            
            /**
             * 敏感词表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}filter_words` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `word` varchar(100) NOT NULL COMMENT '敏感词',
              `replacement` varchar(100) NOT NULL DEFAULT '' COMMENT '替换词',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1启用',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              UNIQUE KEY `word` (`word`),
              KEY `status` (`status`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='敏感词表'",
            
            /**
             * 管理员角色表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}admin_roles` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `name` varchar(50) NOT NULL COMMENT '角色名称',
              `description` varchar(255) NOT NULL DEFAULT '' COMMENT '角色描述',
              `permissions` text COMMENT '权限列表（JSON）',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0禁用 1启用',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              `updated_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              UNIQUE KEY `name` (`name`),
              KEY `status` (`status`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='管理员角色表'",
            
            /**
             * 管理员角色用户关联表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}admin_role_users` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `role_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '角色ID',
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0,
              PRIMARY KEY (`id`),
              KEY `role_id` (`role_id`),
              KEY `user_id` (`user_id`),
              UNIQUE KEY `role_user` (`role_id`, `user_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='管理员角色用户关联表'",
            
            /**
             * 用户黑名单表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}user_blacklist` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `blacklist_user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '被拉黑用户ID',
              `created_at` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `user_blacklist` (`user_id`, `blacklist_user_id`),
              KEY `user_id` (`user_id`),
              KEY `blacklist_user_id` (`blacklist_user_id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户黑名单表'",
            
            /**
             * 用户会话表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}user_sessions` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `user_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
              `session_token` varchar(64) NOT NULL DEFAULT '' COMMENT '会话令牌',
              `device_name` varchar(100) NOT NULL DEFAULT '' COMMENT '设备名称',
              `device_type` varchar(50) NOT NULL DEFAULT '' COMMENT '设备类型：pc/mobile/tablet',
              `ip_address` varchar(50) NOT NULL DEFAULT '' COMMENT 'IP地址',
              `user_agent` varchar(500) NOT NULL DEFAULT '' COMMENT '用户代理',
              `login_time` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '登录时间',
              `last_active` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '最后活跃时间',
              `expire_time` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '过期时间',
              `status` tinyint(1) NOT NULL DEFAULT 1 COMMENT '状态：0失效 1有效',
              PRIMARY KEY (`id`),
              UNIQUE KEY `session_token` (`session_token`),
              KEY `user_id` (`user_id`),
              KEY `status` (`status`),
              KEY `expire_time` (`expire_time`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户会话表'",
            
            /**
             * 通话请求表
             */
            "CREATE TABLE IF NOT EXISTS `{$prefix}call_requests` (
              `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
              `call_id` varchar(64) NOT NULL DEFAULT '' COMMENT '通话ID',
              `caller_id` int(11) NOT NULL DEFAULT 0 COMMENT '呼叫者ID',
              `caller_name` varchar(100) NOT NULL DEFAULT '' COMMENT '呼叫者名称',
              `caller_avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '呼叫者头像',
              `callee_id` int(11) NOT NULL DEFAULT 0 COMMENT '被叫者ID',
              `type` varchar(10) NOT NULL DEFAULT 'voice' COMMENT '通话类型：voice/video',
              `status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT '状态：pending/incoming/connected/ended/rejected',
              `created_at` int(11) NOT NULL DEFAULT 0 COMMENT '创建时间',
              `updated_at` int(11) NOT NULL DEFAULT 0 COMMENT '更新时间',
              PRIMARY KEY (`id`),
              UNIQUE KEY `call_id` (`call_id`),
              KEY `caller_id` (`caller_id`),
              KEY `callee_id` (`callee_id`),
              KEY `status` (`status`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='通话请求表'",
        ];

        foreach ($tables as $sql) {
            $pdo->exec($sql);
        }

        $version = APP_VERSION;
        $time = time();
        $pdo->exec("INSERT INTO `{$prefix}version` (`version`, `created_at`) VALUES ('{$version}', {$time})");
        
        $pdo->exec("INSERT INTO `{$prefix}links` (`name`, `url`, `description`, `sort_order`, `status`, `created_at`, `updated_at`) VALUES ('HuSNS', 'https://huyourui.com', 'HuSNS官方网站', 0, 1, {$time}, {$time})");
        
        $defaultPointRules = [
            ['publish_post', '发布动态', 1, 5],
            ['publish_comment', '发表评论', 1, 10],
            ['receive_like', '被点赞', 1, 20],
            ['receive_comment', '被评论', 2, 10],
            ['follow_user', '关注用户', 1, 10],
            ['be_followed', '被关注', 2, 10],
        ];
        foreach ($defaultPointRules as $rule) {
            $pdo->exec("INSERT INTO `{$prefix}point_rules` (`action`, `name`, `points`, `daily_limit`, `status`, `created_at`, `updated_at`) VALUES ('{$rule[0]}', '{$rule[1]}', {$rule[2]}, {$rule[3]}, 1, {$time}, {$time})");
        }
        
        $defaultSettings = [
            ['max_avatar_size', '5'],
            ['language_mode', 'manual'],
            ['default_language', 'zh-cn'],
            ['available_languages', 'zh-cn,zh-tw,en'],
            ['require_invite_code', '0'],
        ];
        foreach ($defaultSettings as $setting) {
            $pdo->exec("INSERT INTO `{$prefix}settings` (`key`, `value`, `created_at`, `updated_at`) VALUES ('{$setting[0]}', '{$setting[1]}', {$time}, {$time})");
        }
        
        // 初始化敏感词
        $filterWords = ['色情', '暴力', '赌博', '毒品', '诈骗', '传销', '反动', '恐怖', '邪教', '辱骂'];
        foreach ($filterWords as $word) {
            $pdo->exec("INSERT INTO `{$prefix}filter_words` (`word`, `replacement`, `status`, `created_at`, `updated_at`) VALUES ('{$word}', '', 1, {$time}, {$time})");
        }
        
        // 初始化超级管理员角色
        $pdo->exec("INSERT INTO `{$prefix}admin_roles` (`id`, `name`, `description`, `permissions`, `status`, `created_at`, `updated_at`) VALUES (1, '超级管理员', '拥有所有权限', '[]', 1, {$time}, {$time})");
    }

    private function createAdmin($username, $password, $email, $config)
    {
        $dsn = "mysql:host={$config['db_host']};port={$config['db_port']};dbname={$config['db_name']};charset=utf8mb4";
        $pdo = new PDO($dsn, $config['db_user'], $config['db_pass'], [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ]);

        $prefix = $config['db_prefix'];
        $hashPassword = Security::hashPassword($password);
        $time = time();

        $stmt = $pdo->prepare("SELECT id FROM `{$prefix}users` WHERE username = ?");
        $stmt->execute([$username]);
        $exists = $stmt->fetch();

        if ($exists) {
            $stmt = $pdo->prepare("UPDATE `{$prefix}users` SET `password` = ?, `email` = ?, `updated_at` = ? WHERE `username` = ?");
            $stmt->execute([$hashPassword, $email, $time, $username]);
        } else {
            $stmt = $pdo->prepare("INSERT INTO `{$prefix}users` (`username`, `password`, `email`, `avatar`, `is_admin`, `status`, `created_at`, `updated_at`) VALUES (?, ?, ?, 'static/images/avatar_default.png', 1, 1, ?, ?)");
            $stmt->execute([$username, $hashPassword, $email, $time, $time]);
        }
    }

    private function createLockFile()
    {
        file_put_contents(ROOT_PATH . 'install.lock', date('Y-m-d H:i:s'));
    }
}
