<?php

/**
 * 文件功能：创建 vip_levels 会员等级配置表
 * 存储会员等级名称、图标、颜色、倍率、专属模板等配置
 * 后台可完整 CRUD 管理
 */

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
{
    /**
     * 创建 vip_levels 表并填充默认数据
     */
    public function up(): void
    {
        Schema::create('vip_levels', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('会员名称（如：白银会员）');
            $table->string('icon', 20)->default('⭐')->comment('等级图标/emoji');
            $table->string('color', 10)->default('#f59e0b')->comment('等级颜色（hex）');
            $table->decimal('exp_multiplier', 3, 1)->default(1.0)->comment('经验倍率');
            $table->decimal('jjb_multiplier', 3, 1)->default(1.0)->comment('金币倍率');
            $table->text('join_templates')->nullable()->comment('进入聊天室专属欢迎语 JSON 数组');
            $table->text('leave_templates')->nullable()->comment('离开聊天室专属提示语 JSON 数组');
            $table->tinyInteger('sort_order')->default(0)->comment('排序（越大越高级）');
            $table->integer('price')->default(0)->comment('赞助金额（元，供展示参考）');
            $table->integer('duration_days')->default(30)->comment('有效天数（0=永久）');
            $table->timestamps();
        });

        // 默认种子数据（后台可随时修改/删除/新增）
        $now = now();
        DB::table('vip_levels')->insert([
            [
                'name' => '白银会员',
                'icon' => '🥈',
                'color' => '#94a3b8',
                'exp_multiplier' => 1.5,
                'jjb_multiplier' => 1.2,
                'join_templates' => json_encode([
                    '白银贵宾{username}乘坐银色马车缓缓驶入，气质不凡！',
                    '白银贵宾{username}手持银色令牌，大步流星走了进来！',
                ], JSON_UNESCAPED_UNICODE),
                'leave_templates' => json_encode([
                    '白银贵宾{username}挥了挥手，潇洒离去',
                    '白银贵宾{username}骑着银色骏马飘然而去',
                ], JSON_UNESCAPED_UNICODE),
                'sort_order' => 1,
                'price' => 10,
                'duration_days' => 30,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name' => '黄金会员',
                'icon' => '🥇',
                'color' => '#f59e0b',
                'exp_multiplier' => 2.0,
                'jjb_multiplier' => 1.5,
                'join_templates' => json_encode([
                    '黄金贵宾{username}踩着金光闪闪的地毯，王者归来！',
                    '黄金贵宾{username}开着金色豪车呼啸而至，全场瞩目！',
                ], JSON_UNESCAPED_UNICODE),
                'leave_templates' => json_encode([
                    '黄金贵宾{username}乘金色祥云腾空而去，霸气侧漏！',
                    '黄金贵宾{username}微微拱手，金光一闪消失不见',
                ], JSON_UNESCAPED_UNICODE),
                'sort_order' => 2,
                'price' => 30,
                'duration_days' => 30,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name' => '钻石会员',
                'icon' => '💎',
                'color' => '#3b82f6',
                'exp_multiplier' => 3.0,
                'jjb_multiplier' => 2.0,
                'join_templates' => json_encode([
                    '钻石贵宾{username}踏着星辰大海从天而降，万众敬仰！',
                    '钻石贵宾{username}驾驭钻石巨龙破空而来，威震四方！',
                ], JSON_UNESCAPED_UNICODE),
                'leave_templates' => json_encode([
                    '钻石贵宾{username}化作一道星光冲天而去，令人叹服！',
                    '钻石贵宾{username}乘坐钻石飞船消失在银河尽头',
                ], JSON_UNESCAPED_UNICODE),
                'sort_order' => 3,
                'price' => 50,
                'duration_days' => 30,
                'created_at' => $now,
                'updated_at' => $now,
            ],
            [
                'name' => '至尊会员',
                'icon' => '👑',
                'color' => '#a855f7',
                'exp_multiplier' => 5.0,
                'jjb_multiplier' => 3.0,
                'join_templates' => json_encode([
                    '👑至尊{username}御驾亲临！九天雷鸣，万物俯首！众卿接驾！',
                    '👑至尊{username}脚踏七彩祥云，身披紫金龙袍，驾临此地！天地为之变色！',
                ], JSON_UNESCAPED_UNICODE),
                'leave_templates' => json_encode([
                    '👑至尊{username}龙袍一甩，紫气东来，瞬间消失在九天之上！',
                    '👑至尊{username}御驾回宫，百鸟齐鸣相送！',
                ], JSON_UNESCAPED_UNICODE),
                'sort_order' => 4,
                'price' => 100,
                'duration_days' => 30,
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ]);
    }

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