package cloudconfig

import (
	"time"

	"github.com/tidbcloud/devops/models/base"
)

type DefaultConfigurationChangeItem struct {
	ID base.ID `gorm:"primary_key;not null"`

	ItemType string `gorm:"type:VARCHAR(255);index:idx_item_type;not null"`
	Data     []byte `gorm:"type:JSON"`

	// association
	ParentID uint64 `gorm:"index:idx_parent_id;not null"`
}

func (DefaultConfigurationChangeItem) TableName() string {
	return "cloudconfig_default_configuration_change_items"
}

type DefaultConfigurationChangeItemRelease struct {
	ID base.ID `gorm:"primary_key;not null"`

	Environment   string `gorm:"type:VARCHAR(255);not null"`
	ItemID        uint64 `gorm:"index:idx_target_item;not null"`
	Data          []byte `gorm:"type:JSON"`
	ReleaseNumber uint64

	// association
	ParentID uint64 `gorm:"index:idx_parent_id;not null"`
}

func (DefaultConfigurationChangeItemRelease) TableName() string {
	return "cloudconfig_default_configuration_change_item_releases"
}

type DefaultConfigurationChangeRelease struct {
	ID base.ID `gorm:"primary_key;not null"`

	Environment   string     `gorm:"type:VARCHAR(255);not null"`
	Description   string     `gorm:"type:VARCHAR(1024)"`
	ReleasedAt    *time.Time `gorm:"type:TIMESTAMP"`
	ReleaseNumber uint64
	Phase         string `gorm:"type:VARCHAR(255);index:idx_phase;not null"`

	// association
	ApprovalID uint64 `gorm:"index:idx_approval_id;not null"`
	ParentID   uint64 `gorm:"index:idx_parent_id;not null"`
}

func (DefaultConfigurationChangeRelease) TableName() string {
	return "cloudconfig_default_configuration_change_releases"
}

type DefaultConfigurationChange struct {
	base.Updatable

	ClosedAt     *time.Time `gorm:"type:TIMESTAMP"`
	Applicant    string     `gorm:"type:VARCHAR(255);not null"`
	Reason       string     `gorm:"type:VARCHAR(2048);not null"`
	DisplayName  string     `gorm:"type:VARCHAR(255);not null"`
	Phase        string     `gorm:"type:VARCHAR(255);index:idx_phase;not null"`
	PhaseReason  string     `gorm:"type:VARCHAR(2048)"`
	ItemTypes    base.Set   `gorm:"type:set('version_release','template_default_case_binding','default_component_config');index:idx_item_types"`
	Environments base.Set   `gorm:"type:set('dev','staging','prod')"`
}

func (DefaultConfigurationChange) TableName() string {
	return "cloudconfig_default_configuration_changes"
}
