package model

import (
	"time"

	"gorm.io/datatypes"
)

// WeComApprovalContext 保存通用审批实例对应的企微提交安全快照和提交结果。
type WeComApprovalContext struct {
	ID                    uint           `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	ApprovalInstanceID    uint           `gorm:"column:approval_instance_id;not null;uniqueIndex" json:"approval_instance_id"`
	ApplicationID         uint           `gorm:"column:application_id;not null;index" json:"application_id"`
	BusinessType          string         `gorm:"column:business_type;type:varchar(64);not null" json:"business_type"`
	TemplateID            string         `gorm:"column:template_id;type:varchar(128);not null" json:"template_id"`
	CreatorUserID         string         `gorm:"column:creator_userid;type:varchar(64);not null" json:"creator_userid"`
	CreatorName           string         `gorm:"column:creator_name;type:varchar(100);not null" json:"creator_name"`
	CreatorSource         string         `gorm:"column:creator_source;type:varchar(16);not null" json:"creator_source"`
	ControlMapping        datatypes.JSON `gorm:"column:control_mapping;type:jsonb;not null" json:"control_mapping"`
	TemplateSnapshot      datatypes.JSON `gorm:"column:template_snapshot;type:jsonb;not null" json:"template_snapshot"`
	TemplateFingerprint   string         `gorm:"column:template_fingerprint;type:char(64);not null" json:"template_fingerprint"`
	SubmissionStatus      int            `gorm:"column:submission_status;type:int;not null;default:0;index" json:"submission_status"`
	SubmissionAttemptedAt *time.Time     `gorm:"column:submission_attempted_at;type:timestamptz" json:"submission_attempted_at,omitempty"`
	SPNo                  string         `gorm:"column:sp_no;type:varchar(128);not null;default:'';uniqueIndex:uq_wecom_approval_context_sp_no,where:sp_no <> ''" json:"sp_no"`
	LastError             string         `gorm:"column:last_error;type:varchar(500);not null;default:''" json:"last_error"`
	LastRecoveryAt        *time.Time     `gorm:"column:last_recovery_at;type:timestamptz" json:"last_recovery_at,omitempty"`
	LatestSPStatus        int            `gorm:"column:latest_sp_status;type:int;not null;default:0" json:"latest_sp_status"`
	LatestDetailSnapshot  datatypes.JSON `gorm:"column:latest_detail_snapshot;type:jsonb" json:"latest_detail_snapshot,omitempty"`
	LastSyncedAt          *time.Time     `gorm:"column:last_synced_at;type:timestamptz" json:"last_synced_at,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 (WeComApprovalContext) TableName() string {
	return "tb_wecom_approval_context"
}
