package cloudconfig

import (
	"time"

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

type ConfigurationChangeToTenantInit struct {
	ClusterType string `gorm:"type:VARCHAR(255);not null"`
	TenantID    uint64 `gorm:"index:idx_tenant_id;not null"`
	ChangeID    uint64 `gorm:"index:idx_change_id;not null"`
}

func (ConfigurationChangeToTenantInit) TableName() string {
	return "cloudconfig_configuration_change_to_tenant_init"
}

type ConfigurationChangeToCluster struct {
	ClusterType string `gorm:"type:VARCHAR(255);not null"`
	ClusterID   string `gorm:"type:VARCHAR(255);index:idx_cluster_id;not null"`
	ChangeID    uint64 `gorm:"index:idx_change_id;not null"`
}

func (ConfigurationChangeToCluster) TableName() string {
	return "cloudconfig_configuration_change_to_cluster"
}

type ConfigurationChangeToTemplate struct {
	ClusterType  string  `gorm:"type:VARCHAR(255);not null"`
	TemplateType string  `gorm:"type:VARCHAR(255);not null"`
	TenantID     *string `gorm:"type:VARCHAR(255);"`
	ChangeID     uint64  `gorm:"index:idx_change_id;not null"`
}

func (ConfigurationChangeToTemplate) TableName() string {
	return "cloudconfig_configuration_change_to_template"
}

type ConfigurationFileChange struct {
	base.Base

	Filename           string `gorm:"type:VARCHAR(255);not null"`
	ConfigType         string `gorm:"type:VARCHAR(255);not null"`
	TemplateID         uint64 `gorm:""`
	AllParameters      []byte `gorm:"type:JSON;not null"`
	ModifiedParameters []byte `gorm:"type:JSON"`
	ModifiedTemplate   []byte `gorm:"type:JSON"`
	PreviousText       string `gorm:"type:TEXT;not null"`
	ClearCustomParams  bool   `gorm:"not null;default:false"`

	// association
	ChangeID uint64 `gorm:"index:idx_change_id;not null"`
}

func (ConfigurationFileChange) TableName() string {
	return "cloudconfig_configuration_file_changes"
}

type ConfigurationVersionChange struct {
	base.Base

	FromVersion string `gorm:"type:VARCHAR(255);not null"`
	ToVersion   string `gorm:"type:VARCHAR(255);not null"`

	// association
	ChangeID uint64 `gorm:"index:idx_change_id;not null"`
}

func (ConfigurationVersionChange) TableName() string {
	return "cloudconfig_configuration_version_changes"
}

type ConfigurationComponentChange struct {
	base.Base

	Component                 string `gorm:"type:VARCHAR(255);not null"`
	ModifiedVersion           []byte `gorm:"type:JSON"`
	ModifiedImageTag          []byte `gorm:"type:JSON"`
	ModifiedOverwriteImageTag []byte `gorm:"type:JSON"`

	// association
	ChangeID uint64 `gorm:"index:idx_change_id;not null"`
}

func (ConfigurationComponentChange) TableName() string {
	return "cloudconfig_configuration_component_changes"
}

type ConfigurationChange struct {
	base.Updatable

	TargetType     string `gorm:"type:VARCHAR(255);not null"`
	TargetVersion  string `gorm:"type:VARCHAR(255);not null"`
	ChangeType     string `gorm:"type:VARCHAR(255);not null"`
	Reason         string `gorm:"type:VARCHAR(1024);not null"`
	Applicant      string `gorm:"type:VARCHAR(255);not null"`
	OperatorEntity string `gorm:"type:VARCHAR(255)"`
	ApplyPolicy    string `gorm:"type:VARCHAR(255);not null"`

	Phase      string     `gorm:"type:VARCHAR(255);index:idx_phase;not null"`
	ApprovalID *uint64    `gorm:"index:idx_approval_id"`
	ClosedAt   *time.Time `gorm:"type:TIMESTAMP"`
	AppliedAt  *time.Time `gorm:"type:TIMESTAMP"`

	ExecutionWindowID *uint64 `gorm:"column:execution_window_id"`

	PremiumUpgradeStepID *uint64 `gorm:"type:BIGINT UNSIGNED;uniqueIndex:uk_cloudconfig_premium_upgrade_step"`

	// association
	Files        []*ConfigurationFileChange       `gorm:"foreignkey:ChangeID"`
	Components   []*ConfigurationComponentChange  `gorm:"foreignkey:ChangeID"`
	Version      *ConfigurationVersionChange      `gorm:"foreignkey:ChangeID"`
	ToTenantInit *ConfigurationChangeToTenantInit `gorm:"foreignkey:ChangeID"`
	ToCluster    *ConfigurationChangeToCluster    `gorm:"foreignkey:ChangeID"`
	ToTemplate   *ConfigurationChangeToTemplate   `gorm:"foreignkey:ChangeID"`
}

func (ConfigurationChange) TableName() string {
	return "cloudconfig_configuration_changes"
}
