package dispatch

import (
	"time"

	modbase "github.com/tidbcloud/aisupport/services/ticket-manager/models/base"
)

type Execution struct {
	modbase.Updatable

	IssueKey          string     `gorm:"column:issue_key;type:varchar(64);not null;index:idx_dispatch_execution_issue;index:idx_dispatch_execution_running,priority:1"`
	Mode              string     `gorm:"column:mode;type:varchar(16);not null;index:idx_dispatch_execution_running,priority:3"`
	PolicyID          *uint64    `gorm:"column:policy_id;type:bigint unsigned;index:idx_dispatch_execution_running,priority:2"`
	Status            string     `gorm:"column:status;type:varchar(32);not null;index:idx_dispatch_execution_status;index:idx_dispatch_execution_running,priority:4"`
	FailureCode       string     `gorm:"column:failure_code;type:varchar(128);not null;default:''"`
	FailureReason     string     `gorm:"column:failure_reason;type:text;not null"`
	OverwriteDataJSON []byte     `gorm:"column:overwrite_data_json;type:json"`
	CompletedAt       *time.Time `gorm:"column:completed_at;type:datetime(3)"`
}

func (Execution) TableName() string {
	return "dispatch_executions"
}

type RuleExecution struct {
	ID modbase.ID `gorm:"primary_key;not null"`

	ExecutionID          modbase.ID `gorm:"column:execution_id;type:bigint unsigned;not null;uniqueIndex:idx_dispatch_rule_execution_execution_sequence,priority:1;index:idx_dispatch_rule_execution_execution"`
	Sequence             int        `gorm:"column:sequence;type:int;not null;uniqueIndex:idx_dispatch_rule_execution_execution_sequence,priority:2"`
	RuleID               modbase.ID `gorm:"column:rule_id;type:bigint unsigned;not null;index:idx_dispatch_rule_execution_rule"`
	Outcome              string     `gorm:"column:outcome;type:varchar(32);not null"`
	Reason               string     `gorm:"column:reason;type:text;not null"`
	ActionExecutionsJSON []byte     `gorm:"column:action_executions_json;type:json;not null"`
}

func (RuleExecution) TableName() string {
	return "dispatch_rule_executions"
}
