package entity

import (
	"time"

	"github.com/tidbcloud/serverless-service/internal/domain/constant"
)

// Identifier columns kept at varchar(128) so that 3- and 4-column
// composite indexes stay well under MySQL's 3072-byte (utf8mb4) key
// limit. cluster_id / changefeed_id are uuid-shaped, cloud_provider /
// region / kubernetes / ticdc_cluster_name are short K8s/AWS names —
// 128 chars is plenty.
type TiCDCChangefeedResource struct {
	ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`

	ClusterID    string `gorm:"column:cluster_id;type:varchar(128);not null;uniqueIndex:udx_cluster_changefeed,priority:1"`
	ChangefeedID string `gorm:"column:changefeed_id;type:varchar(128);not null;uniqueIndex:udx_cluster_changefeed,priority:2"`
	// idx_ticdc_resource_shard is the lookup for ListByShard; it must
	// start with cloud_provider (the leftmost prefix of the WHERE clause),
	// so cluster_id is intentionally not part of this index.
	CloudProvider         string `gorm:"column:cloud_provider;type:varchar(64);not null;index:idx_ticdc_resource_shard,priority:1"`
	Region                string `gorm:"column:region;type:varchar(64);not null;index:idx_ticdc_resource_shard,priority:2"`
	Kubernetes            string `gorm:"column:kubernetes;type:varchar(128);not null;index:idx_ticdc_resource_shard,priority:3"`
	ShareScope            string `gorm:"column:share_scope;type:varchar(32);not null;default:'global';index:idx_ticdc_resource_shard,priority:4"`
	ShareKey              string `gorm:"column:share_key;type:varchar(128);not null;default:'global';index:idx_ticdc_resource_shard,priority:5"`
	TiCDCClusterName      string `gorm:"column:ticdc_cluster_name;type:varchar(128);not null"`
	ExpectedSpecification string `gorm:"column:expected_specification;type:varchar(64);not null;default:''"`
	ActualSpecification   string `gorm:"column:actual_specification;type:varchar(64);not null;default:''"`

	CreatedAt time.Time  `gorm:"column:created_at;type:timestamp;not null;autoCreateTime"`
	UpdatedAt *time.Time `gorm:"column:updated_at;type:timestamp;null;default:null;autoUpdateTime"`
}

// TiCDCCluster is the per-shard shared TiCDC cluster record.
//
// One row per (cloud_provider, region, kubernetes) shard. The
// shared cluster's spec is the aggregation of all active changefeeds'
// expected_specification on the shard.
//
// Version is captured at create time from the resolved newarch
// regional/global version and is NOT auto-refreshed when the config
// file changes — config-side canaries don't silently roll every
// dataplane. Only the explicit ChangefeedOpsService.UpgradeTiCDCCluster
// ops API writes a new Version (and flips state -> upgrading).
type TiCDCCluster struct {
	ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`

	CloudProvider    string                     `gorm:"column:cloud_provider;type:varchar(64);not null;uniqueIndex:udx_ticdc_cluster_shard,priority:1"`
	Region           string                     `gorm:"column:region;type:varchar(64);not null;uniqueIndex:udx_ticdc_cluster_shard,priority:2"`
	Kubernetes       string                     `gorm:"column:kubernetes;type:varchar(128);not null;uniqueIndex:udx_ticdc_cluster_shard,priority:3"`
	ShareScope       string                     `gorm:"column:share_scope;type:varchar(32);not null;default:'global';uniqueIndex:udx_ticdc_cluster_shard,priority:4"`
	ShareKey         string                     `gorm:"column:share_key;type:varchar(128);not null;default:'global';uniqueIndex:udx_ticdc_cluster_shard,priority:5"`
	TiCDCClusterName string                     `gorm:"column:ticdc_cluster_name;type:varchar(128);not null;uniqueIndex:udx_ticdc_cluster_name"`
	Version          string                     `gorm:"column:version;type:varchar(64);not null;default:''"`
	ExpectedSpec     string                     `gorm:"column:expected_specification;type:varchar(64);not null;default:''"`
	ActualSpec       string                     `gorm:"column:actual_specification;type:varchar(64);not null;default:''"`
	State            constant.TiCDCClusterState `gorm:"column:state;type:varchar(32);not null;default:'creating'"`

	CreatedAt time.Time  `gorm:"column:created_at;type:timestamp;not null;autoCreateTime"`
	UpdatedAt *time.Time `gorm:"column:updated_at;type:timestamp;null;default:null;autoUpdateTime"`
	DeletedAt *time.Time `gorm:"column:deleted_at;type:timestamp;null"`
}

type KernelTiCDCChangefeed struct {
	ID uint64 `gorm:"column:id;primaryKey;autoIncrement"`

	ClusterID          string                               `gorm:"column:cluster_id;type:varchar(128);not null;uniqueIndex:udx_cluster_changefeed,priority:1"`
	ChangefeedID       string                               `gorm:"column:changefeed_id;type:varchar(128);not null;uniqueIndex:udx_cluster_changefeed,priority:2"`
	CloudProvider      string                               `gorm:"column:cloud_provider;type:varchar(64);not null;index:idx_kernel_ticdc_changefeed_shard,priority:1"`
	Region             string                               `gorm:"column:region;type:varchar(64);not null;index:idx_kernel_ticdc_changefeed_shard,priority:2"`
	Kubernetes         string                               `gorm:"column:kubernetes;type:varchar(128);not null;index:idx_kernel_ticdc_changefeed_shard,priority:3"`
	ShareScope         string                               `gorm:"column:share_scope;type:varchar(32);not null;default:'global';index:idx_kernel_ticdc_changefeed_shard,priority:4"`
	ShareKey           string                               `gorm:"column:share_key;type:varchar(128);not null;default:'global';index:idx_kernel_ticdc_changefeed_shard,priority:5"`
	KeySpaceID         uint32                               `gorm:"column:keyspace_id;type:bigint(20);not null"`
	TiCDCClusterName   string                               `gorm:"column:ticdc_cluster_name;type:varchar(128);not null"`
	KernelChangefeedID string                               `gorm:"column:kernel_changefeed_id;type:varchar(128);not null;uniqueIndex:udx_kernel_changefeed_id"`
	KernelStatus       constant.TiCDCChangefeedKernelStatus `gorm:"column:kernel_status;type:varchar(32);not null;default:'pending'"`

	CreatedAt time.Time  `gorm:"column:created_at;type:timestamp;not null;autoCreateTime"`
	UpdatedAt *time.Time `gorm:"column:updated_at;type:timestamp;null;default:null;autoUpdateTime"`
	DeletedAt *time.Time `gorm:"column:deleted_at;type:timestamp;null"`
}
