<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('failed_messages', function (Blueprint $table) {
            $table->id()->comment('主键');
            $table->string('error_id', 50)->unique()->comment('错误唯一标识（err_xxx）');
            $table->string('data_type', 30)->comment('数据类型: order/product/refund');
            $table->string('platform', 50)->nullable()->comment('平台名称');
            $table->unsignedBigInteger('platform_id')->nullable()->comment('平台 ID');
            $table->unsignedBigInteger('company_id')->nullable()->comment('公司 ID');
            $table->unsignedBigInteger('store_id')->nullable()->comment('店铺 ID');
            $table->string('error_type', 255)->comment('异常类名');
            $table->text('error_message')->comment('异常消息');
            $table->integer('error_code')->default(0)->comment('异常代码');
            $table->text('error_trace')->comment('异常堆栈');
            $table->jsonb('original_message')->comment('原始消息体');
            $table->integer('retry_count')->default(0)->comment('重试次数');
            $table->string('message_id', 100)->nullable()->comment('消息 ID');
            $table->timestampTz('failed_at')->comment('失败时间');
            $table->timestampTz('created_at')->useCurrent()->comment('创建时间');

            $table->index('data_type');
            $table->index('platform_id');
            $table->index('failed_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('failed_messages');
    }
};
