package model

import (
	"time"

	"gorm.io/datatypes"
)

// OutboxEvent 是跨进程可靠事件的权威公共持久化模型。
type OutboxEvent struct {
	ID               uint           `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	EventID          string         `gorm:"column:event_id;type:varchar(64);not null;uniqueIndex:uq_outbox_event_id" json:"event_id"`
	EventType        string         `gorm:"column:event_type;type:varchar(150);not null;index:idx_outbox_event_type_status,priority:1" json:"event_type"`
	PayloadVersion   int            `gorm:"column:payload_version;type:int;not null;default:1" json:"payload_version"`
	AggregateType    string         `gorm:"column:aggregate_type;type:varchar(100);not null" json:"aggregate_type"`
	AggregateID      string         `gorm:"column:aggregate_id;type:varchar(100);not null" json:"aggregate_id"`
	ResourceType     string         `gorm:"column:resource_type;type:varchar(100);not null" json:"resource_type"`
	ResourceID       string         `gorm:"column:resource_id;type:varchar(100);not null" json:"resource_id"`
	BusinessKey      string         `gorm:"column:business_key;type:varchar(150);not null;default:''" json:"business_key,omitempty"`
	RequestID        string         `gorm:"column:request_id;type:varchar(100);not null;default:'';index" json:"request_id,omitempty"`
	CorrelationID    string         `gorm:"column:correlation_id;type:varchar(100);not null;default:'';index" json:"correlation_id,omitempty"`
	Payload          datatypes.JSON `gorm:"column:payload;type:jsonb;not null" json:"payload"`
	Status           int            `gorm:"column:status;type:int;not null;default:1;index:idx_outbox_event_type_status,priority:2" json:"status"`
	RetryCount       int            `gorm:"column:retry_count;type:int;not null;default:0" json:"retry_count"`
	MaxRetries       int            `gorm:"column:max_retries;type:int;not null;default:10" json:"max_retries"`
	NextAttemptAt    time.Time      `gorm:"column:next_attempt_at;type:timestamptz;not null;index:idx_outbox_event_claim,priority:2" json:"next_attempt_at"`
	LeaseOwner       *string        `gorm:"column:lease_owner;type:varchar(100)" json:"lease_owner,omitempty"`
	LeaseExpiresAt   *time.Time     `gorm:"column:lease_expires_at;type:timestamptz;index:idx_outbox_event_claim,priority:3" json:"lease_expires_at,omitempty"`
	LastErrorCode    string         `gorm:"column:last_error_code;type:varchar(100);not null;default:''" json:"last_error_code,omitempty"`
	LastErrorSummary string         `gorm:"column:last_error_summary;type:varchar(500);not null;default:''" json:"last_error_summary,omitempty"`
	DeliveredAt      *time.Time     `gorm:"column:delivered_at;type:timestamptz" json:"delivered_at,omitempty"`
	CreatedAt        time.Time      `gorm:"column:created_at;type:timestamptz;not null;autoCreateTime;index:idx_outbox_event_claim,priority:4" json:"created_at"`
	UpdatedAt        time.Time      `gorm:"column:updated_at;type:timestamptz;not null;autoUpdateTime" json:"updated_at"`
}

// TableName 返回公共 Outbox 表名。
func (OutboxEvent) TableName() string {
	return "tb_outbox_event"
}
