<?php

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

class CreatePaymentRetriesTable extends Migration
{
    /**
     * 运行迁移
     *
     * @return void
     */
    public function up()
    {
        Schema::create('payment_retries', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('payment_record_id')->comment('支付记录ID');
            $table->integer('attempts')->default(0)->comment('尝试次数');
            $table->integer('max_attempts')->default(3)->comment('最大尝试次数');
            $table->integer('delay')->default(60)->comment('延迟时间(秒)');
            $table->float('backoff_factor')->default(2.0)->comment('退避因子');
            $table->integer('max_delay')->default(3600)->comment('最大延迟时间(秒)');
            $table->string('status')->default('pending')->comment('状态');
            $table->text('last_error')->nullable()->comment('最后错误信息');
            $table->text('last_result')->nullable()->comment('最后结果');
            $table->timestamps();

            $table->foreign('payment_record_id')->references('id')->on('payment_records')->onDelete('cascade');
        });
    }

    /**
     * 回滚迁移
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('payment_retries');
    }
}
