<?php

/**
 * 文件功能：创建聊天室座驾独立数据表。
 *
 * 座驾定义和用户座驾购买记录独立于商店模块，支持后台单独配置价格、使用天数和欢迎语。
 */

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
{
    /**
     * 方法功能：创建 rides 与 user_ride_purchases 并预置默认座驾。
     */
    public function up(): void
    {
        Schema::create('rides', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100)->comment('座驾名称');
            $table->string('slug', 100)->unique()->comment('座驾唯一标识，格式 ride_key');
            $table->string('effect_key', 50)->unique()->comment('全屏特效 key');
            $table->string('icon', 20)->default('🚘')->comment('座驾图标');
            $table->text('description')->nullable()->comment('座驾说明');
            $table->unsignedInteger('price')->default(0)->comment('购买价格');
            $table->unsignedInteger('duration_days')->default(7)->comment('使用天数');
            $table->string('welcome_message', 255)->nullable()->comment('入场欢迎语模板');
            $table->unsignedInteger('sort_order')->default(0)->comment('排序权重');
            $table->boolean('is_active')->default(true)->comment('是否上架');
            $table->timestamps();
        });

        Schema::create('user_ride_purchases', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('ride_id')->constrained('rides')->cascadeOnDelete();
            $table->enum('status', ['active', 'expired', 'cancelled'])->default('active')->comment('座驾状态');
            $table->unsignedInteger('price_paid')->default(0)->comment('实际支付金币');
            $table->timestamp('expires_at')->nullable()->comment('到期时间');
            $table->timestamp('used_at')->nullable()->comment('首次使用时间');
            $table->timestamps();

            $table->index(['user_id', 'status', 'expires_at']);
        });

        $this->seedDefaultRides();
    }

    /**
     * 方法功能：删除座驾独立数据表。
     */
    public function down(): void
    {
        Schema::dropIfExists('user_ride_purchases');
        Schema::dropIfExists('rides');
    }

    /**
     * 方法功能：写入当前默认四个全屏座驾。
     */
    private function seedDefaultRides(): void
    {
        $now = now();
        $rides = [
            [
                'name' => '歼-35隐身战机',
                'slug' => 'ride_j35',
                'effect_key' => 'j35',
                'description' => '驾驶歼-35划破长空入场，附带全屏战机掠过特效。',
                'icon' => '🛩️',
                'price' => 18888,
                'duration_days' => 7,
                'sort_order' => 80,
                'welcome_message' => '【{name}】驾驶【{ride}】划破长空，震撼降临聊天室！',
            ],
            [
                'name' => '99A主战坦克',
                'slug' => 'ride_99a',
                'effect_key' => '99a',
                'description' => '驾驶 99A 主战坦克重装入场，附带履带尘土与炮击冲击特效。',
                'icon' => '🛡️',
                'price' => 18888,
                'duration_days' => 7,
                'sort_order' => 81,
                'welcome_message' => '【{name}】驾驶【{ride}】重装入场，地面都为之一震！',
            ],
            [
                'name' => '东风-5C战略导弹',
                'slug' => 'ride_df5c',
                'effect_key' => 'df5c',
                'description' => '乘东风-5C 发射升空入场，附带尾焰、烟尘和雷达 HUD 特效。',
                'icon' => '🚀',
                'price' => 28888,
                'duration_days' => 7,
                'sort_order' => 82,
                'welcome_message' => '【{name}】乘【{ride}】点火升空，战略级排面拉满！',
            ],
            [
                'name' => '福建舰航母',
                'slug' => 'ride_fujian',
                'effect_key' => 'fujian',
                'description' => '乘福建舰破浪入场，附带海浪、舰载机和甲板 HUD 特效。',
                'icon' => '⚓',
                'price' => 28888,
                'duration_days' => 7,
                'sort_order' => 83,
                'welcome_message' => '【{name}】乘【{ride}】破浪而来，全场列队欢迎！',
            ],
        ];

        foreach ($rides as $ride) {
            DB::table('rides')->insert($ride + [
                'is_active' => true,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }
    }
};
