package db

import (
	"github.com/tidbcloud/cluster-service-ng/pkg/storage/mysql/schema"
	"gorm.io/gorm"
)

// 20260713001: create the autoscale policy tables.
//
// One consolidated migration that creates all three tables for the
// autoscale policy feature introduced in PR #2856 / #2918:
//
//   - autoscale_policies — retained dormant for backward compatibility.
//     Under the storage-point exclusivity invariant (design doc
//     §Storage-point exclusivity), SHARED_POOL/CLUSTER policy reads go
//     through readRuntimeSpecs (HPA `spec.behavior` + `ru_configs.FeatureFlags`);
//     this table is not read or written for SHARED_POOL/CLUSTER.
//     `ApplyMaterializedAutoscalePolicies` is deprecated and rejects all
//     callers at the service boundary.
//   - autoscale_runtime_guards — SRE region-guard kill switch
//     (FORBID_SCALE_IN at REGION scope; store-only this iteration — no
//     runtime enforcement).
//   - autoscale_policy_bundle_specs — canonical bundle store for the
//     policy RPCs (CreateOrReplace / Patch / Get / GetEffective / List /
//     Preview / Sync). One row per (scope, subject_id).
//
// AutoMigrate is idempotent: re-running the migration is a no-op
// once the tables exist.
func init() {
	AddDBMigrateRecord("20260713001", func(db *gorm.DB) error {
		if err := db.AutoMigrate(
			&schema.AutoscalePolicy{},
			&schema.AutoscaleRuntimeGuard{},
			&schema.AutoscalePolicyBundleSpec{},
		); err != nil {
			return err
		}
		return nil
	})
}
