// Package autonumberseed holds the pure, backend-free autonumber-seed DTOs,
// the declared-seed key, the LegacySource enum strings, and the
// BuildScopeSourceMapFromSeeds translator — relocated out of internal/bootstrap
// as the Wave-0 SLICE B slice of the bootstrap-decoupling campaign.
//
// Everything here is stdlib-only. The DB-driven seed runner
// (SeedAutoNumberCounters) and the V1-V8 verify gate
// (VerifyAutoNumberInvariants) read the live DB and remain in
// internal/bootstrap as behavioral/port symbols.
package autonumberseed

// AutoNumberFormatSeed mirrors packs.AutoNumberFormatSeed but lives in
// the bootstrap package so SeedAutoNumberCounters does not create a
// packs → bootstrap → packs import cycle (packs already depends on
// bootstrap for ReconcileTableColumns / InitializeFieldMappings).
//
// L0 platform seeds and app-pack wire-up code construct this slice through the
// controlplane/packs shim; tests in this package build it directly.
type AutoNumberFormatSeed struct {
	ObjectName   string
	FieldApiName string
	Format       string
	Scope        string
	Start        int64
	LegacySource string
}

// LegacySource constants — short enum values used by scopeSourceMap
// (per Wave 3 Lane C2 task spec). Pack seed structs may use the longer
// table-name form (`_System_AutoNumber`, `_System_CompoundAutoNumber`);
// callers are free to use either convention so long as both ends agree.
// This file accepts both.
const (
	LegacySourceAutoNumber = "auto_number"
	LegacySourceCompound   = "compound"
	LegacySourceNone       = "none"

	legacyTableAutoNumber = "_System_AutoNumber"
	legacyTableCompound   = "_System_CompoundAutoNumber"
)

// AutoNumberDeclaredSeedKey identifies one declared autonumber seed by
// its target (ObjectName, FieldApiName) pair. Registered via
// RegisterAutoNumberDeclaredSeeds so V8 can compute an accurate
// declared-vs-reachable expected count without creating a
// bootstrap → controlplane/packs import cycle.
type AutoNumberDeclaredSeedKey struct {
	ObjectName   string
	FieldApiName string
}

// BuildScopeSourceMapFromSeeds is a convenience translator for callers that
// want to derive the scopeSourceMap directly from declared seeds. It converts
// the long table-name form into the short enum.
func BuildScopeSourceMapFromSeeds(seeds []AutoNumberFormatSeed) map[string]string {
	out := make(map[string]string, len(seeds))
	for _, s := range seeds {
		key := s.ObjectName + "." + s.FieldApiName
		switch s.LegacySource {
		case legacyTableAutoNumber:
			out[key] = LegacySourceAutoNumber
		case legacyTableCompound:
			out[key] = LegacySourceCompound
		case "", LegacySourceNone:
			out[key] = LegacySourceNone
		default:
			// Unknown — preserve raw value; SeedAutoNumberCounters
			// will surface it as an error at runtime.
			out[key] = s.LegacySource
		}
	}
	return out
}
