<?php

/**
 * 文件功能：创建商店相关表
 * shop_items — 商品定义（特效卡、周卡、改名卡等）
 * user_purchases — 用户购买记录（含状态与有效期）
 * username_blacklist — 用户改名后的旧名称保留黑名单
 *
 * @author   ChatRoom Laravel
 *
 * @version  1.0.0
 */

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
{
    /**
     * 创建商店相关三张表并填充默认商品
     */
    public function up(): void
    {
        // ── 商品表 ────────────────────────────────────────────────────
        Schema::create('shop_items', function (Blueprint $table) {
            $table->id();
            $table->string('name', 50)->comment('商品名称');
            $table->string('slug', 50)->unique()->comment('唯一标识（如 once_fireworks）');
            $table->string('description', 255)->nullable()->comment('商品描述');
            $table->string('icon', 20)->nullable()->comment('显示图标 emoji');
            $table->unsignedInteger('price')->default(0)->comment('金币价格');
            $table->string('type', 20)->default('instant')->comment('类型：instant=单次 / duration=周卡 / rename=改名');
            $table->unsignedTinyInteger('duration_days')->nullable()->comment('有效天数（duration 类型）');
            $table->unsignedTinyInteger('sort_order')->default(0)->comment('排序（越小越靠前）');
            $table->boolean('is_active')->default(true)->comment('是否上架');
            $table->timestamps();
        });

        // 填充默认商品
        DB::table('shop_items')->insert([
            ['name' => '🎆 单次烟花',   'slug' => 'once_fireworks',  'description' => '全场燃放一次烟花特效',   'icon' => '🎆', 'price' => 888,  'type' => 'instant',  'duration_days' => null, 'sort_order' => 1, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '🌧️ 单次下雨',   'slug' => 'once_rain',       'description' => '全场下一次雨特效',       'icon' => '🌧️', 'price' => 888,  'type' => 'instant',  'duration_days' => null, 'sort_order' => 2, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '⚡ 单次雷电',   'slug' => 'once_lightning',  'description' => '全场一次雷电特效',       'icon' => '⚡', 'price' => 888,  'type' => 'instant',  'duration_days' => null, 'sort_order' => 3, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '❄️ 单次下雪',   'slug' => 'once_snow',       'description' => '全场下一次雪特效',       'icon' => '❄️', 'price' => 888,  'type' => 'instant',  'duration_days' => null, 'sort_order' => 4, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '🎆 烟花周卡',   'slug' => 'week_fireworks',  'description' => '7天内进房间自动放烟花',  'icon' => '🎆', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7,    'sort_order' => 5, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '🌧️ 下雨周卡',   'slug' => 'week_rain',       'description' => '7天内进房间自动下雨',    'icon' => '🌧️', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7,    'sort_order' => 6, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '⚡ 雷电周卡',   'slug' => 'week_lightning',  'description' => '7天内进房间自动雷电',    'icon' => '⚡', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7,    'sort_order' => 7, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '❄️ 下雪周卡',   'slug' => 'week_snow',       'description' => '7天内进房间自动下雪',    'icon' => '❄️', 'price' => 8888, 'type' => 'duration', 'duration_days' => 7,    'sort_order' => 8, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
            ['name' => '✏️ 改名卡',      'slug' => 'rename_card',     'description' => '修改一次用户名（受黑名单限制）', 'icon' => '✏️', 'price' => 5000, 'type' => 'one_time', 'duration_days' => null, 'sort_order' => 9, 'is_active' => true, 'created_at' => now(), 'updated_at' => now()],
        ]);

        // ── 购买记录表 ────────────────────────────────────────────────
        Schema::create('user_purchases', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->index()->comment('购买用户 ID');
            $table->unsignedBigInteger('shop_item_id')->comment('商品 ID');
            $table->string('status', 20)->default('active')->comment('状态：active / used / expired');
            $table->unsignedInteger('price_paid')->default(0)->comment('实际支付金币数');
            $table->timestamp('expires_at')->nullable()->comment('过期时间（duration 类型有效）');
            $table->timestamp('used_at')->nullable()->comment('使用时间（instant 类型有效）');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
            $table->foreign('shop_item_id')->references('id')->on('shop_items')->cascadeOnDelete();
        });

        // ── 用户名黑名单表 ────────────────────────────────────────────
        Schema::create('username_blacklist', function (Blueprint $table) {
            $table->id();
            $table->string('username', 50)->index()->comment('被保留的旧用户名');
            $table->timestamp('reserved_until')->nullable()->comment('保留截止时间（过期后可被注册）');
            $table->timestamp('created_at')->nullable()->comment('写入时间');
        });
    }

    /**
     * 回滚：删除商店相关三张表
     */
    public function down(): void
    {
        Schema::dropIfExists('user_purchases');
        Schema::dropIfExists('shop_items');
        Schema::dropIfExists('username_blacklist');
    }
};
