package model

import (
	"time"

	"gorm.io/datatypes"
)

// IntegrationLog 是外部交互及未发送尝试的权威持久化模型。
type IntegrationLog struct {
	ID               uint           `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	IntegrationID    string         `gorm:"column:integration_id;type:varchar(64);not null;uniqueIndex" json:"integration_id"`
	IdempotencyKey   *string        `gorm:"column:idempotency_key;type:varchar(160)" json:"idempotency_key,omitempty"`
	Provider         string         `gorm:"column:provider;type:varchar(32);not null" json:"provider"`
	Direction        string         `gorm:"column:direction;type:varchar(16);not null" json:"direction"`
	Operation        string         `gorm:"column:operation;type:varchar(64);not null" json:"operation"`
	ExternalID       *string        `gorm:"column:external_id;type:varchar(128)" json:"external_id,omitempty"`
	ResourceType     *string        `gorm:"column:resource_type;type:varchar(64)" json:"resource_type,omitempty"`
	ResourceID       *string        `gorm:"column:resource_id;type:varchar(128)" json:"resource_id,omitempty"`
	ResourceKey      *string        `gorm:"column:resource_key;type:varchar(128)" json:"resource_key,omitempty"`
	TriggerSource    *string        `gorm:"column:trigger_source;type:varchar(32)" json:"trigger_source,omitempty"`
	TriggerScene     *string        `gorm:"column:trigger_scene;type:varchar(128)" json:"trigger_scene,omitempty"`
	TriggerSeries    *string        `gorm:"column:trigger_series;type:varchar(64)" json:"trigger_series,omitempty"`
	ScheduledAt      *time.Time     `gorm:"column:scheduled_at;type:timestamptz" json:"scheduled_at,omitempty"`
	StartedAt        *time.Time     `gorm:"column:started_at;type:timestamptz" json:"started_at,omitempty"`
	Attempt          int            `gorm:"column:attempt;type:int;not null;default:1" json:"attempt"`
	Result           string         `gorm:"column:result;type:varchar(20);not null" json:"result"`
	HTTPStatus       *int           `gorm:"column:http_status" json:"http_status,omitempty"`
	ProviderCode     *string        `gorm:"column:provider_code;type:varchar(64)" json:"provider_code,omitempty"`
	ProviderMessage  *string        `gorm:"column:provider_message;type:varchar(500)" json:"provider_message,omitempty"`
	RequestSummary   datatypes.JSON `gorm:"column:request_summary;type:jsonb" json:"request_summary,omitempty"`
	ResponseSummary  datatypes.JSON `gorm:"column:response_summary;type:jsonb" json:"response_summary,omitempty"`
	ContentHash      string         `gorm:"column:content_hash;type:varchar(64);not null;default:''" json:"content_hash,omitempty"`
	DurationMS       int64          `gorm:"column:duration_ms;not null;default:0" json:"duration_ms"`
	StateChanged     bool           `gorm:"column:state_changed;not null;default:false" json:"state_changed"`
	Metadata         datatypes.JSON `gorm:"column:metadata;type:jsonb" json:"metadata,omitempty"`
	RecoveryStrategy *string        `gorm:"column:recovery_strategy;type:varchar(255)" json:"recovery_strategy,omitempty"`
	RequestID        *string        `gorm:"column:request_id;type:varchar(64)" json:"request_id,omitempty"`
	CorrelationID    *string        `gorm:"column:correlation_id;type:varchar(64)" json:"correlation_id,omitempty"`
	AuditEventID     *uint          `gorm:"column:audit_event_id" json:"audit_event_id,omitempty"`
	CreatedAt        time.Time      `gorm:"column:created_at;type:timestamptz;not null;autoCreateTime" json:"created_at"`
	UpdatedAt        time.Time      `gorm:"column:updated_at;type:timestamptz;not null;autoUpdateTime" json:"updated_at"`
}

// TableName 返回外部集成日志表名。
func (IntegrationLog) TableName() string {
	return "tb_integration_log"
}
