<?php

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

/**
 * 文件功能：创建 VIP 支付订单表迁移
 * 为聊天室 VIP 在线支付提供本地订单存储能力
 */
return new class extends Migration
{
    /**
     * 运行迁移
     * 创建 VIP 支付订单表，记录本地订单与远端支付单的映射关系
     */
    public function up(): void
    {
        Schema::create('vip_payment_orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_no', 32)->unique()->comment('本地 VIP 支付订单号');
            $table->string('merchant_order_no', 32)->unique()->comment('提交给支付中心的业务订单号');
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete()->comment('购买用户');
            $table->foreignId('vip_level_id')->constrained('vip_levels')->restrictOnDelete()->comment('目标 VIP 等级');
            $table->string('status', 20)->default('created')->index()->comment('订单状态：created|pending|paid|closed|failed');
            $table->decimal('amount', 10, 2)->comment('支付金额');
            $table->string('subject', 120)->comment('支付标题');
            $table->string('payment_order_no', 32)->nullable()->index()->comment('NovaLink 平台支付单号');
            $table->string('provider', 30)->nullable()->comment('支付渠道，例如 alipay');
            $table->string('provider_trade_no', 80)->nullable()->comment('第三方支付流水号');
            $table->string('vip_name', 60)->comment('下单时快照的 VIP 名称');
            $table->unsignedInteger('vip_duration_days')->default(0)->comment('下单时快照的会员时长');
            $table->json('sync_return_payload')->nullable()->comment('同步回调原始数据');
            $table->json('async_notify_payload')->nullable()->comment('异步回调原始数据');
            $table->timestamp('paid_at')->nullable()->comment('支付成功时间');
            $table->timestamp('opened_vip_at')->nullable()->comment('实际开通会员时间');
            $table->json('meta')->nullable()->comment('扩展信息');
            $table->timestamps();
        });
    }

    /**
     * 回滚迁移
     * 删除 VIP 支付订单表
     */
    public function down(): void
    {
        Schema::dropIfExists('vip_payment_orders');
    }
};
