<?php

/**
 * 文件功能：创建系统参数表（sysparam）并填充所有默认配置
 * 合并原来分散的多个 seed 迁移，统一在此处完成
 */

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * 创建 sysparam 表并一次性填入所有系统参数（最终值）
     */
    public function up(): void
    {
        Schema::create('sysparam', function (Blueprint $table) {
            $table->id();
            $table->string('alias', 100)->unique()->comment('参数别名（唯一标识）');
            $table->text('body')->nullable()->comment('参数值');
            $table->string('guidetxt', 500)->nullable()->comment('参数说明（后台显示）');
            $table->timestamps();
        });

        // 生成 99 级经验阶梯（level^2.5 曲线，取整到十位）
        $thresholds = [];
        for ($level = 1; $level <= 99; $level++) {
            $thresholds[] = (int) (ceil(10 * pow($level, 2.5) / 10) * 10);
        }
        $levelExpStr = implode(',', $thresholds);

        $now = now();

        DB::table('sysparam')->insert([
            // ── 等级体系（99 级）────────────────────────────────────────
            ['alias' => 'levelexp',        'body' => $levelExpStr,  'guidetxt' => '等级经验阈值配置（逗号分隔，第N个=升到N级所需累计经验）',     'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'maxlevel',        'body' => '99',          'guidetxt' => '用户最高可达等级',                                              'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'superlevel',      'body' => '100',         'guidetxt' => '管理员级别（= 最高等级 + 1，拥有最高权限的等级阈值）',          'created_at' => $now, 'updated_at' => $now],

            // ── 心跳奖励 ──────────────────────────────────────────────
            ['alias' => 'exp_per_heartbeat', 'body' => '1',        'guidetxt' => '⚡ 每次心跳经验奖励（支持固定值如"1"或范围如"1-10"；0关闭）', 'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'jjb_per_heartbeat', 'body' => '1-3',      'guidetxt' => '💰 每次心跳金币奖励（支持固定值如"1"或范围如"1-5"；0关闭）', 'created_at' => $now, 'updated_at' => $now],

            // ── 管理操作权限等级 ──────────────────────────────────────
            ['alias' => 'level_warn',       'body' => '5',          'guidetxt' => '警告所需等级',                                                  'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'level_mute',       'body' => '50',         'guidetxt' => '禁言所需等级',                                                  'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'level_kick',       'body' => '60',         'guidetxt' => '踢人所需等级',                                                  'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'level_announcement', 'body' => '60',        'guidetxt' => '设置公告所需等级',                                              'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'level_ban',        'body' => '80',         'guidetxt' => '封号所需等级',                                                  'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'level_banip',      'body' => '90',         'guidetxt' => '封IP所需等级',                                                  'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'level_freeze',     'body' => '14',         'guidetxt' => '冻结账号所需等级',                                              'created_at' => $now, 'updated_at' => $now],

            // ── 随机事件 ──────────────────────────────────────────────
            ['alias' => 'auto_event_chance', 'body' => '10',         'guidetxt' => '随机事件触发概率（百分比，1-100）',                             'created_at' => $now, 'updated_at' => $now],

            // ── 魅力系统 ──────────────────────────────────────────────
            ['alias' => 'charm_cross_sex',  'body' => '2',          'guidetxt' => '异性聊天每条消息增加的魅力值',                                  'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'charm_same_sex',   'body' => '1',          'guidetxt' => '同性聊天每条消息增加的魅力值',                                  'created_at' => $now, 'updated_at' => $now],
            ['alias' => 'charm_hourly_limit', 'body' => '20',        'guidetxt' => '每小时通过聊天获取的魅力值上限（防刷屏）',                      'created_at' => $now, 'updated_at' => $now],

            // ── 排行榜 ────────────────────────────────────────────────
            ['alias' => 'leaderboard_limit', 'body' => '20',         'guidetxt' => '🏆 排行榜每榜显示人数',                                         'created_at' => $now, 'updated_at' => $now],
        ]);
    }

    /**
     * 回滚：删除 sysparam 表
     */
    public function down(): void
    {
        Schema::dropIfExists('sysparam');
    }
};
