package records

import "context"

type runtimeReservedCreateIDContextKey struct{}

// withRuntimeReservedCreateID is private to the Runtime command adapter. The
// value is injected after external field authorization has already treated Id
// as non-createable, so it cannot become a client-controlled system-field
// bypass or a new public Records capability.
func withRuntimeReservedCreateID(ctx context.Context, id string) context.Context {
	if id == "" {
		return ctx
	}
	return context.WithValue(ctx, runtimeReservedCreateIDContextKey{}, id)
}

// withoutRuntimeReservedCreateID removes the command adapter's outer-create
// reservation before persistence invokes any nested write hooks. The outer
// record already owns the reserved Id in its prepared row image by then;
// allowing descendants to inherit the reservation would make unrelated child
// inserts reuse the outer record's identity.
func withoutRuntimeReservedCreateID(ctx context.Context) context.Context {
	if ctx == nil || runtimeReservedCreateID(ctx) == "" {
		return ctx
	}
	return context.WithValue(ctx, runtimeReservedCreateIDContextKey{}, "")
}

func runtimeReservedCreateID(ctx context.Context) string {
	if ctx == nil {
		return ""
	}
	id, _ := ctx.Value(runtimeReservedCreateIDContextKey{}).(string)
	return id
}
