package rootcoord

import (
	"context"
	"strings"

	"github.com/cockroachdb/errors"
	"github.com/samber/lo"
	"google.golang.org/protobuf/types/known/fieldmaskpb"

	"github.com/milvus-io/milvus-proto/go-api/v3/commonpb"
	"github.com/milvus-io/milvus-proto/go-api/v3/milvuspb"
	"github.com/milvus-io/milvus-proto/go-api/v3/schemapb"
	"github.com/milvus-io/milvus/internal/distributed/streaming"
	"github.com/milvus-io/milvus/internal/metastore/model"
	"github.com/milvus-io/milvus/internal/streamingcoord/server/broadcaster"
	"github.com/milvus-io/milvus/internal/streamingcoord/server/broadcaster/registry"
	"github.com/milvus-io/milvus/internal/util/hookutil"
	"github.com/milvus-io/milvus/pkg/v3/common"
	"github.com/milvus-io/milvus/pkg/v3/mlog"
	"github.com/milvus-io/milvus/pkg/v3/proto/indexpb"
	"github.com/milvus-io/milvus/pkg/v3/proto/messagespb"
	"github.com/milvus-io/milvus/pkg/v3/proto/proxypb"
	"github.com/milvus-io/milvus/pkg/v3/proto/querypb"
	"github.com/milvus-io/milvus/pkg/v3/streaming/util/message"
	"github.com/milvus-io/milvus/pkg/v3/streaming/util/message/ce"
	"github.com/milvus-io/milvus/pkg/v3/util/funcutil"
	"github.com/milvus-io/milvus/pkg/v3/util/merr"
	"github.com/milvus-io/milvus/pkg/v3/util/timestamptz"
	"github.com/milvus-io/milvus/pkg/v3/util/typeutil"
)

// broadcastAlterCollectionForAlterCollection broadcasts the put collection message for alter collection.
func (c *Core) broadcastAlterCollectionForAlterCollection(ctx context.Context, req *milvuspb.AlterCollectionRequest) error {
	if req.GetCollectionName() == "" {
		return merr.WrapErrParameterInvalidMsg("alter collection failed, collection name does not exists")
	}

	if len(req.GetProperties()) == 0 && len(req.GetDeleteKeys()) == 0 {
		return merr.WrapErrParameterInvalidMsg("no properties or delete keys provided")
	}

	if len(req.GetProperties()) > 0 && len(req.GetDeleteKeys()) > 0 {
		return merr.WrapErrParameterInvalidMsg("can not provide properties and deletekeys at the same time")
	}

	if err := validateReservedCollectionProperties(req.GetProperties(), req.GetDeleteKeys()); err != nil {
		return err
	}

	if hookutil.ContainsCipherProperties(req.GetProperties(), req.GetDeleteKeys()) {
		return merr.WrapErrParameterInvalidMsg("can not alter cipher related properties")
	}

	if err := common.ValidateNamespaceShardingEnabledNotAltered(req.GetProperties(), req.GetDeleteKeys()); err != nil {
		return err
	}

	if err := validateNamespaceModeImmutable(req.GetProperties(), req.GetDeleteKeys()); err != nil {
		return err
	}

	if funcutil.SliceContain(req.GetDeleteKeys(), common.EnableDynamicSchemaKey) {
		return merr.WrapErrParameterInvalidMsg("cannot delete key %s, dynamic field schema could support set to true/false", common.EnableDynamicSchemaKey)
	}

	// Validate timezone
	tz, exist := funcutil.TryGetAttrByKeyFromRepeatedKV(common.TimezoneKey, req.GetProperties())
	if exist && !timestamptz.IsTimezoneValid(tz) {
		return merr.WrapErrParameterInvalidMsg("unknown or invalid IANA Time Zone ID: %s", tz)
	}

	isEnableDynamicSchema, targetValue, err := common.IsEnableDynamicSchema(req.GetProperties())
	if err != nil {
		rawValue, _ := funcutil.TryGetAttrByKeyFromRepeatedKV(common.EnableDynamicSchemaKey, req.GetProperties())
		return merr.WrapErrParameterInvalidMsg("invalid dynamic schema property value: %s", rawValue)
	}
	if isEnableDynamicSchema {
		// if there's dynamic schema property, it will add a new dynamic field into the collection.
		// the property cannot be seen at collection properties, only add a new field into the collection.
		return c.broadcastAlterCollectionForAlterDynamicField(ctx, req, targetValue)
	}

	broadcaster, err := c.startBroadcastWithAliasOrCollectionLock(ctx, req.GetDbName(), req.GetCollectionName())
	if err != nil {
		return err
	}
	defer broadcaster.Close()

	// check if the collection exists
	coll, err := c.meta.GetCollectionByName(ctx, req.GetDbName(), req.GetCollectionName(), typeutil.MaxTimestamp, false)
	if err != nil {
		return err
	}

	cacheExpirations, err := c.getCacheExpireForCollection(ctx, req.GetDbName(), req.GetCollectionName())
	if err != nil {
		return err
	}
	header := &messagespb.AlterCollectionMessageHeader{
		DbId:         coll.DBID,
		CollectionId: coll.CollectionID,
		UpdateMask: &fieldmaskpb.FieldMask{
			Paths: []string{},
		},
		CacheExpirations: cacheExpirations,
	}
	udpates := &messagespb.AlterCollectionMessageUpdates{}

	// Apply the properties to override the existing properties.
	oldProperties := common.CloneKeyValuePairs(coll.Properties).ToMap()
	newProperties := common.CloneKeyValuePairs(coll.Properties).ToMap()
	for _, prop := range req.GetProperties() {
		switch prop.GetKey() {
		case common.CollectionDescription:
			if prop.GetValue() != coll.Description {
				udpates.Description = prop.GetValue()
				header.UpdateMask.Paths = append(header.UpdateMask.Paths, message.FieldMaskCollectionDescription)
			}
		case common.ConsistencyLevel:
			if lv, ok := unmarshalConsistencyLevel(prop.GetValue()); ok && lv != coll.ConsistencyLevel {
				udpates.ConsistencyLevel = lv
				header.UpdateMask.Paths = append(header.UpdateMask.Paths, message.FieldMaskCollectionConsistencyLevel)
			}
		case common.CollectionExternalSource:
			if udpates.Schema == nil {
				udpates.Schema = &schemapb.CollectionSchema{}
			}
			udpates.Schema.ExternalSource = prop.GetValue()
			if !funcutil.SliceContain(header.UpdateMask.Paths, message.FieldMaskCollectionExternalSpec) {
				header.UpdateMask.Paths = append(header.UpdateMask.Paths, message.FieldMaskCollectionExternalSpec)
			}
		case common.CollectionExternalSpec:
			if udpates.Schema == nil {
				udpates.Schema = &schemapb.CollectionSchema{}
			}
			udpates.Schema.ExternalSpec = prop.GetValue()
			if !funcutil.SliceContain(header.UpdateMask.Paths, message.FieldMaskCollectionExternalSpec) {
				header.UpdateMask.Paths = append(header.UpdateMask.Paths, message.FieldMaskCollectionExternalSpec)
			}
		default:
			newProperties[prop.GetKey()] = prop.GetValue()
		}
	}
	for _, deleteKey := range req.GetDeleteKeys() {
		delete(newProperties, deleteKey)
	}

	// Check if the properties are changed.
	newPropsKeyValuePairs := common.NewKeyValuePairs(newProperties)
	if !newPropsKeyValuePairs.Equal(coll.Properties) {
		udpates.Properties = newPropsKeyValuePairs
		header.UpdateMask.Paths = append(header.UpdateMask.Paths, message.FieldMaskCollectionProperties)
	}

	// If TTL field is changed through properties, also broadcast an updated schema snapshot and mark it as schema change,
	// so QueryNode can refresh runtime schema properties without requiring release/load.
	ttlOld, okOld := oldProperties[common.CollectionTTLFieldKey]
	ttlNew, okNew := newProperties[common.CollectionTTLFieldKey]
	needTTLFieldSchemaRefresh := (okOld != okNew) || (okOld && okNew && ttlOld != ttlNew)
	if needTTLFieldSchemaRefresh {
		// validate ttl field name exists in schema fields when setting it
		if okNew {
			found := false
			for _, f := range coll.Fields {
				if f.Name == ttlNew {
					found = true
					break
				}
			}
			if !found {
				return merr.WrapErrParameterInvalidMsg("ttl field name %s not found in schema", ttlNew)
			}
		}

		// Ensure schema update mask exists so QueryNode pipeline treats this as a schema update event.
		if !funcutil.SliceContain(header.UpdateMask.Paths, message.FieldMaskCollectionSchema) {
			header.UpdateMask.Paths = append(header.UpdateMask.Paths, message.FieldMaskCollectionSchema)
		}

		// Build schema snapshot with updated properties (schema version should NOT be changed for properties-only alter).
		schema := coll.ToCollectionSchemaPB()
		schema.Properties = newPropsKeyValuePairs
		// Preserve ExternalSource/ExternalSpec from current collection state
		// unless this alter is itself updating them (refresh-completion sync).
		if udpates.Schema != nil && udpates.Schema.ExternalSource != "" {
			schema.ExternalSource = udpates.Schema.ExternalSource
		}
		if udpates.Schema != nil && udpates.Schema.ExternalSpec != "" {
			schema.ExternalSpec = udpates.Schema.ExternalSpec
		}
		udpates.Schema = schema
	}

	// if there's no change, return nil directly to promise idempotent.
	if len(header.UpdateMask.Paths) == 0 {
		return errIgnoredAlterCollection
	}

	// fill the put load config if rg or replica number is changed.
	udpates.AlterLoadConfig = c.getAlterLoadConfigOfAlterCollection(coll.Properties, udpates.Properties)

	channels := make([]string, 0, len(coll.VirtualChannelNames)+1)
	channels = append(channels, streaming.WAL().ControlChannel())
	channels = append(channels, coll.VirtualChannelNames...)
	msg := message.NewAlterCollectionMessageBuilderV2().
		WithHeader(header).
		WithBody(&messagespb.AlterCollectionMessageBody{
			Updates: udpates,
		}).
		WithBroadcast(channels).
		MustBuildBroadcast()
	if _, err := broadcaster.Broadcast(ctx, msg); err != nil {
		return err
	}
	return nil
}

func validateReservedCollectionProperties(properties []*commonpb.KeyValuePair, deleteKeys []string) error {
	for _, property := range properties {
		if property.GetKey() == common.MaxFieldIDKey {
			return merr.WrapErrParameterInvalidMsg("cannot alter reserved collection property %s", common.MaxFieldIDKey)
		}
	}
	if funcutil.SliceContain(deleteKeys, common.MaxFieldIDKey) {
		return merr.WrapErrParameterInvalidMsg("cannot delete reserved collection property %s", common.MaxFieldIDKey)
	}
	return nil
}

func validateNamespaceModeImmutable(properties []*commonpb.KeyValuePair, deleteKeys []string) error {
	for _, prop := range properties {
		if prop.GetKey() == common.NamespaceModeKey {
			return merr.WrapErrParameterInvalidMsg("cannot alter %s via alter_collection_properties; namespace mode is immutable after collection creation", common.NamespaceModeKey)
		}
		if strings.EqualFold(prop.GetKey(), common.NamespaceModeKey) {
			return merr.WrapErrParameterInvalidMsg("invalid property key %q, did you mean %q?", prop.GetKey(), common.NamespaceModeKey)
		}
	}
	for _, key := range deleteKeys {
		if key == common.NamespaceModeKey {
			return merr.WrapErrParameterInvalidMsg("cannot delete %s; namespace mode is immutable after collection creation", common.NamespaceModeKey)
		}
		if strings.EqualFold(key, common.NamespaceModeKey) {
			return merr.WrapErrParameterInvalidMsg("invalid property key %q, did you mean %q?", key, common.NamespaceModeKey)
		}
	}
	return nil
}

// broadcastAlterCollectionForAlterDynamicField broadcasts the put collection message for alter dynamic field.
func (c *Core) broadcastAlterCollectionForAlterDynamicField(ctx context.Context, req *milvuspb.AlterCollectionRequest, targetValue bool) error {
	if len(req.GetProperties()) != 1 {
		return merr.WrapErrParameterInvalidMsg("cannot alter dynamic schema with other properties at the same time")
	}

	coll, err := c.meta.GetCollectionByName(ctx, req.GetDbName(), req.GetCollectionName(), typeutil.MaxTimestamp, false)
	if err != nil {
		return err
	}
	if coll.EnableDynamicField == targetValue {
		return errIgnoredAlterCollection
	}
	if !targetValue {
		if err := waitUntilSchemaDropReady(ctx); err != nil {
			return err
		}
	}

	broadcaster, err := c.startBroadcastWithCollectionLock(ctx, req.GetDbName(), coll.Name)
	if err != nil {
		return err
	}
	defer broadcaster.Close()

	coll, err = c.meta.GetCollectionByName(ctx, req.GetDbName(), req.GetCollectionName(), typeutil.MaxTimestamp, false)
	if err != nil {
		return err
	}
	if coll.EnableDynamicField == targetValue {
		return errIgnoredAlterCollection
	}

	// Disable dynamic field: remove $meta field from schema.
	if !targetValue {
		return c.broadcastDisableDynamicField(ctx, req, coll, broadcaster)
	}

	// convert to add $meta json field, nullable, default value `{}`
	fieldSchema := &schemapb.FieldSchema{
		Name:      common.MetaFieldName,
		DataType:  schemapb.DataType_JSON,
		IsDynamic: true,
		Nullable:  true,
		DefaultValue: &schemapb.ValueField{
			Data: &schemapb.ValueField_BytesData{
				BytesData: []byte("{}"),
			},
		},
	}
	if err := checkFieldSchema([]*schemapb.FieldSchema{fieldSchema}); err != nil {
		return err
	}

	schema := coll.ToCollectionSchemaPB()
	fieldSchema.FieldID = maxAssignedFieldIDFromSchema(schema) + 1
	schema.Version = coll.SchemaVersion + 1
	schema.EnableDynamicField = targetValue
	schema.Fields = append(schema.Fields, fieldSchema)
	properties := updateMaxFieldIDProperty(coll.Properties, fieldSchema.GetFieldID())
	schema.Properties = properties
	if err := validateSchemaEvolution(coll, schema); err != nil {
		return err
	}

	channels := make([]string, 0, len(coll.VirtualChannelNames)+1)
	channels = append(channels, streaming.WAL().ControlChannel())
	channels = append(channels, coll.VirtualChannelNames...)
	cacheExpirations, err := c.getCacheExpireForCollection(ctx, req.GetDbName(), req.GetCollectionName())
	if err != nil {
		return err
	}
	// broadcast the put collection v2 message.
	msg := message.NewAlterCollectionMessageBuilderV2().
		WithHeader(&messagespb.AlterCollectionMessageHeader{
			DbId:         coll.DBID,
			CollectionId: coll.CollectionID,
			UpdateMask: &fieldmaskpb.FieldMask{
				Paths: []string{message.FieldMaskCollectionSchema, message.FieldMaskCollectionProperties},
			},
			CacheExpirations: cacheExpirations,
		}).
		WithBody(&messagespb.AlterCollectionMessageBody{
			Updates: &messagespb.AlterCollectionMessageUpdates{
				Schema:     schema,
				Properties: properties,
			},
		}).
		WithBroadcast(channels).
		MustBuildBroadcast()
	if _, err := broadcaster.Broadcast(ctx, msg); err != nil {
		return err
	}
	return nil
}

// broadcastDisableDynamicField removes the $meta field to disable dynamic schema.
func (c *Core) broadcastDisableDynamicField(ctx context.Context, req *milvuspb.AlterCollectionRequest, coll *model.Collection, bc broadcaster.BroadcastAPI) error {
	// Find and remove $meta field, record its ID for cascade index cleanup.
	fields := model.MarshalFieldModels(coll.Fields)
	var dynamicFieldID int64
	newFields := make([]*schemapb.FieldSchema, 0, len(fields))
	for _, f := range fields {
		if f.IsDynamic {
			dynamicFieldID = f.FieldID
		} else {
			newFields = append(newFields, f)
		}
	}
	if dynamicFieldID == 0 {
		return merr.WrapErrParameterInvalidMsg("dynamic field not found")
	}

	schema := coll.ToCollectionSchemaPB()
	maxFieldID := maxAssignedFieldIDFromSchema(schema)
	properties := updateMaxFieldIDProperty(coll.Properties, maxFieldID)
	schema.Fields = newFields
	schema.EnableDynamicField = false
	schema.Properties = properties
	schema.Version = coll.SchemaVersion + 1
	if err := validateSchemaEvolution(coll, schema); err != nil {
		return err
	}

	channels := make([]string, 0, len(coll.VirtualChannelNames)+1)
	channels = append(channels, streaming.WAL().ControlChannel())
	channels = append(channels, coll.VirtualChannelNames...)
	cacheExpirations, err := c.getCacheExpireForCollection(ctx, req.GetDbName(), req.GetCollectionName())
	if err != nil {
		return err
	}
	msg := message.NewAlterCollectionMessageBuilderV2().
		WithHeader(&messagespb.AlterCollectionMessageHeader{
			DbId:         coll.DBID,
			CollectionId: coll.CollectionID,
			UpdateMask: &fieldmaskpb.FieldMask{
				Paths: []string{
					message.FieldMaskCollectionSchema,
					message.FieldMaskCollectionProperties,
				},
			},
			CacheExpirations: cacheExpirations,
			DroppedFieldIds:  []int64{dynamicFieldID},
		}).
		WithBody(&messagespb.AlterCollectionMessageBody{
			Updates: &messagespb.AlterCollectionMessageUpdates{
				Schema:     schema,
				Properties: properties,
			},
		}).
		WithBroadcast(channels).
		MustBuildBroadcast()
	if _, err := bc.Broadcast(ctx, msg); err != nil {
		return err
	}
	return nil
}

// getCacheExpireForCollection gets the cache expirations for collection.
func (c *Core) getCacheExpireForCollection(ctx context.Context, dbName string, collectionNameOrAlias string) (*message.CacheExpirations, error) {
	coll, err := c.meta.GetCollectionByName(ctx, dbName, collectionNameOrAlias, typeutil.MaxTimestamp, false)
	if err != nil {
		return nil, err
	}
	aliases, err := c.meta.ListAliases(ctx, dbName, coll.Name, typeutil.MaxTimestamp)
	if err != nil {
		return nil, err
	}
	builder := ce.NewBuilder()
	builder.WithLegacyProxyCollectionMetaCache(
		ce.OptLPCMDBName(dbName),
		ce.OptLPCMCollectionName(coll.Name),
		ce.OptLPCMCollectionID(coll.CollectionID),
		ce.OptLPCMMsgType(commonpb.MsgType_AlterCollection),
	)
	for _, alias := range aliases {
		builder.WithLegacyProxyCollectionMetaCache(
			ce.OptLPCMDBName(dbName),
			ce.OptLPCMCollectionName(alias),
			ce.OptLPCMCollectionID(coll.CollectionID),
			ce.OptLPCMMsgType(commonpb.MsgType_AlterAlias),
		)
	}
	return builder.Build(), nil
}

// getAlterLoadConfigOfAlterCollection gets the alter load config of alter collection.
func (c *Core) getAlterLoadConfigOfAlterCollection(oldProps []*commonpb.KeyValuePair, newProps []*commonpb.KeyValuePair) *message.AlterLoadConfigOfAlterCollection {
	oldReplicaNumber, _ := common.CollectionLevelReplicaNumber(oldProps)
	oldResourceGroups, _ := common.CollectionLevelResourceGroups(oldProps)
	newReplicaNumber, _ := common.CollectionLevelReplicaNumber(newProps)
	newResourceGroups, _ := common.CollectionLevelResourceGroups(newProps)
	left, right := lo.Difference(oldResourceGroups, newResourceGroups)
	rgChanged := len(left) > 0 || len(right) > 0
	replicaChanged := oldReplicaNumber != newReplicaNumber
	if !replicaChanged && !rgChanged {
		return nil
	}

	return &message.AlterLoadConfigOfAlterCollection{
		ReplicaNumber:  int32(newReplicaNumber),
		ResourceGroups: newResourceGroups,
	}
}

func (c *DDLCallback) alterCollectionV2AckCallback(ctx context.Context, result message.BroadcastResultAlterCollectionMessageV2) error {
	header := result.Message.Header()
	body := result.Message.MustBody()
	if err := c.meta.AlterCollection(ctx, result); err != nil {
		if errors.Is(err, errAlterCollectionNotFound) {
			mlog.Warn(ctx, "alter a non-existent collection, ignore it", mlog.FieldMessage(result.Message))
			return nil
		}
		return merr.Wrap(err, "failed to alter collection")
	}
	// Refresh datacoord's cached collection schema BEFORE the bound index meta
	// becomes visible: creating the index signals the index inspector, whose
	// function-output-field guard reads that cached schema — on a stale view it
	// would schedule doomed builds on segments that have no binlog for the new
	// field yet. The schema push depends only on rootcoord meta (updated above),
	// never on index meta, so this order is always safe.
	if err := c.broker.BroadcastAlteredCollection(ctx, header.CollectionId); err != nil {
		return merr.Wrap(err, "failed to broadcast altered collection")
	}
	if err := c.applyBoundFieldIndexesInline(ctx, result); err != nil {
		return err
	}
	if body.Updates.AlterLoadConfig != nil {
		resp, err := c.mixCoord.UpdateLoadConfig(ctx, &querypb.UpdateLoadConfigRequest{
			CollectionIDs:  []int64{header.CollectionId},
			ReplicaNumber:  body.Updates.AlterLoadConfig.ReplicaNumber,
			ResourceGroups: body.Updates.AlterLoadConfig.ResourceGroups,
		})
		if err != nil {
			return merr.Wrap(err, "failed to update load config")
		}
		if err := merr.CheckRPCCall(resp, err); err != nil {
			if errors.Is(err, merr.ErrResourceGroupNotFound) {
				mlog.Warn(ctx, "failed to update load config due to missing resource group, stop retrying", mlog.Err(err))
				return nil
			}
			return merr.Wrap(err, "failed to update load config")
		}
	}
	if err := c.cascadeDropFieldIndexesInline(ctx, result); err != nil {
		return err
	}

	// If the collection was renamed or moved to a different DB, grants were migrated
	// in MetaTable.AlterCollection. Refresh the RBAC policy cache on all proxies so
	// they pick up the new grant keys.
	for _, path := range header.UpdateMask.GetPaths() {
		if path == message.FieldMaskCollectionName || path == message.FieldMaskDB {
			if err := c.proxyClientManager.RefreshPolicyInfoCache(ctx, &proxypb.RefreshPolicyInfoCacheRequest{
				OpType: int32(typeutil.CacheRefresh),
			}); err != nil {
				mlog.Warn(ctx, "failed to refresh RBAC policy cache after collection rename, skipping", mlog.Err(err))
			}
			break
		}
	}

	return c.ExpireCaches(ctx, header)
}

// applyBoundFieldIndexesInline creates the index meta bound to a newly added
// function-output field by inlining the CreateIndex ack callback, same pattern as
// cascadeDropFieldIndexesInline. The FieldIndex was fully materialized (id/name
// allocated, params validated) at DDL prepare time, so this is a pure idempotent
// apply: a replayed callback rebuilds the identical synthetic message. Cannot use
// the CreateIndex RPC here because it would deadlock on the resource key lock.
// The synthetic message is never appended to the WAL; it only routes the apply
// through the registry to datacoord's createIndexV2AckCallback.
func (c *DDLCallback) applyBoundFieldIndexesInline(ctx context.Context, result message.BroadcastResultAlterCollectionMessageV2) error {
	header := result.Message.Header()
	boundFieldIndexes := result.Message.MustBody().GetUpdates().GetBoundFieldIndexes()
	if len(boundFieldIndexes) == 0 {
		return nil
	}

	controlChannelResult := result.GetControlChannelResult()
	for _, fieldIndex := range boundFieldIndexes {
		indexInfo := fieldIndex.GetIndexInfo()
		mlog.Info(ctx, "applying bound field index of alter collection schema",
			mlog.FieldMessage(result.Message),
			mlog.FieldFieldID(indexInfo.GetFieldID()),
			mlog.String("indexName", indexInfo.GetIndexName()),
			mlog.FieldIndexID(indexInfo.GetIndexID()),
		)
		createIndexMsg := message.NewCreateIndexMessageBuilderV2().
			WithHeader(&message.CreateIndexMessageHeader{
				DbId:         header.DbId,
				CollectionId: header.CollectionId,
				FieldId:      indexInfo.GetFieldID(),
				IndexId:      indexInfo.GetIndexID(),
				IndexName:    indexInfo.GetIndexName(),
			}).
			WithBody(&message.CreateIndexMessageBody{
				FieldIndex: fieldIndex,
			}).
			WithBroadcast([]string{streaming.WAL().ControlChannel()}).
			MustBuildBroadcast().
			WithBroadcastID(result.Message.BroadcastHeader().BroadcastID)

		if err := registry.CallMessageAckCallback(ctx, createIndexMsg, map[string]*message.AppendResult{
			streaming.WAL().ControlChannel(): controlChannelResult,
		}); err != nil {
			return merr.Wrap(err, "failed to apply bound field index")
		}
	}
	return nil
}

// cascadeDropFieldIndexesInline drops indexes on dropped fields by inlining the
// DropIndex ack callback, same pattern as dropCollectionV1AckCallback.
// Cannot use DropIndex RPC here because it would deadlock on the resource key lock.
func (c *DDLCallback) cascadeDropFieldIndexesInline(ctx context.Context, result message.BroadcastResultAlterCollectionMessageV2) error {
	header := result.Message.Header()
	droppedFieldIDs := header.GetDroppedFieldIds()
	if len(droppedFieldIDs) == 0 {
		return nil
	}

	resp, err := c.mixCoord.DescribeIndex(ctx, &indexpb.DescribeIndexRequest{
		CollectionID: header.CollectionId,
		IndexName:    "",
	})
	if err := merr.CheckRPCCall(resp.GetStatus(), err); err != nil {
		if merr.ErrIndexNotFound.Is(err) {
			return nil
		}
		return errors.Wrap(err, "failed to describe indexes for cascade drop")
	}

	droppedFieldSet := make(map[int64]struct{}, len(droppedFieldIDs))
	for _, fid := range droppedFieldIDs {
		droppedFieldSet[fid] = struct{}{}
	}
	var indexIDs []int64
	for _, indexInfo := range resp.GetIndexInfos() {
		if _, ok := droppedFieldSet[indexInfo.GetFieldID()]; ok {
			mlog.Info(ctx, "cascade dropping index on dropped field",
				mlog.FieldMessage(result.Message),
				mlog.FieldFieldID(indexInfo.GetFieldID()),
				mlog.String("indexName", indexInfo.GetIndexName()),
				mlog.FieldIndexID(indexInfo.GetIndexID()),
			)
			indexIDs = append(indexIDs, indexInfo.GetIndexID())
		}
	}
	if len(indexIDs) == 0 {
		return nil
	}

	controlChannelResult := result.GetControlChannelResult()
	dropIndexMsg := message.NewDropIndexMessageBuilderV2().
		WithHeader(&message.DropIndexMessageHeader{
			CollectionId: header.CollectionId,
			IndexIds:     indexIDs,
		}).
		WithBody(&message.DropIndexMessageBody{}).
		WithBroadcast([]string{streaming.WAL().ControlChannel()}).
		MustBuildBroadcast().
		WithBroadcastID(result.Message.BroadcastHeader().BroadcastID)

	if err := registry.CallMessageAckCallback(ctx, dropIndexMsg, map[string]*message.AppendResult{
		streaming.WAL().ControlChannel(): controlChannelResult,
	}); err != nil {
		return errors.Wrap(err, "failed to cascade drop field indexes")
	}
	return nil
}
