package testutil

import (
	"testing"

	"gorm.io/gorm"
)

// CreateTemporaryOutboxTable 创建事务内自动回滚的公共 Outbox 测试表。
func CreateTemporaryOutboxTable(t *testing.T, db *gorm.DB) {
	t.Helper()
	if err := db.Exec(`CREATE TEMP TABLE tb_outbox_event (
		id bigserial PRIMARY KEY, event_id varchar(64) NOT NULL UNIQUE,
		event_type varchar(150) NOT NULL, payload_version integer NOT NULL,
		aggregate_type varchar(100) NOT NULL, aggregate_id varchar(100) NOT NULL,
		resource_type varchar(100) NOT NULL, resource_id varchar(100) NOT NULL,
		business_key varchar(150) NOT NULL DEFAULT '', request_id varchar(100) NOT NULL DEFAULT '',
		correlation_id varchar(100) NOT NULL DEFAULT '', payload jsonb NOT NULL,
		status integer NOT NULL, retry_count integer NOT NULL DEFAULT 0,
		max_retries integer NOT NULL, next_attempt_at timestamptz NOT NULL,
		lease_owner varchar(100), lease_expires_at timestamptz,
		last_error_code varchar(100) NOT NULL DEFAULT '', last_error_summary varchar(500) NOT NULL DEFAULT '',
		delivered_at timestamptz, created_at timestamptz NOT NULL DEFAULT NOW(), updated_at timestamptz NOT NULL DEFAULT NOW()
	) ON COMMIT DROP`).Error; err != nil {
		t.Fatalf("创建 Outbox 测试表失败：%v", err)
	}
}
