package analytics

import (
	"context"

	domainSchema "github.com/nexuscrm/kernel/pkg/schema"
)

// indexedRollupMetadata is an optional metadata capability. Independent
// Runtime views implement it from their immutable rollup index; legacy and
// test metadata sources retain the existing full-schema fallback.
type indexedRollupMetadata interface {
	RollupChildDependencies(context.Context, string) []domainSchema.RollupChildDependency
	HasRollupChildDependencies(context.Context, string) bool
}

func rollupChildDependencies(ctx context.Context, source MetadataSource, childObjectAPIName string) []domainSchema.RollupChildDependency {
	if source == nil {
		return nil
	}
	if indexed, ok := source.(indexedRollupMetadata); ok {
		return indexed.RollupChildDependencies(ctx, childObjectAPIName)
	}
	return domainSchema.FindRollupChildDependencies(source.GetSchemas(ctx), childObjectAPIName)
}

func hasRollupChildDependencies(ctx context.Context, source MetadataSource, childObjectAPIName string) bool {
	if source == nil {
		return false
	}
	if indexed, ok := source.(indexedRollupMetadata); ok {
		return indexed.HasRollupChildDependencies(ctx, childObjectAPIName)
	}
	return domainSchema.HasRollupChildDependencies(source.GetSchemas(ctx), childObjectAPIName)
}
