<?php

declare(strict_types=1);


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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->id();
            $table->string('order_no', 40)->unique();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('subject', 255);
            $table->string('description', 500)->nullable();
            $table->unsignedBigInteger('amount'); // 单位：分
            $table->string('channel', 20)->default('alipay'); // alipay / wechat / sandbox
            $table->string('status', 20)->default('pending'); // pending / paid / failed / closed
            $table->string('gateway_trade_no', 100)->nullable();
            $table->json('payload')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->timestamps();

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

    public function down(): void
    {
        Schema::dropIfExists('payments');
    }
};
