// Package packs — bootstrap_shim.go exposes the generated
// AutoNumberFormatSeeds slice to callers in the bootstrap domain without
// creating a packs → bootstrap → packs import cycle (packs already
// imports bootstrap for ReconcileTableColumns / InitializeFieldMappings).
//
// The generated `packs.AutoNumberFormatSeed` struct and the domain
// `bootstrap.AutoNumberFormatSeed` struct are wire-identical but live in
// separate packages. This shim performs the one-line copy, preserving
// the F8 `LegacySource` column that earlier Lane-D wire-ups dropped.
//
// Round-5 Codex V1-P1 / V2-C1: the previous phase5 path used
// `loadAutoNumberSeedsFromDB` which read only 5 columns and silently
// defaulted `LegacySource` to `"none"`. That made scope-aware GREATEST
// fall back to `max(live+1, start)` for every seed, ignoring the
// legacy `_System_AutoNumber.CurrentNumber` and
// `_System_CompoundAutoNumber.CurrentNumber` floors. On cutover this
// would silently re-issue already-used identifiers.
package packs

import "github.com/nexuscrm/backend/internal/bootstrap"

// BootstrapSeeds returns every generated AutoNumberFormatSeed projected
// into the bootstrap package's AutoNumberFormatSeed type. The output
// preserves every column — critically including LegacySource — so that
// `bootstrap.BuildScopeSourceMapFromSeeds` can emit the correct
// scope-source map for `SeedAutoNumberCounters`.
//
// Callers MUST use this (or reproduce the same copy) when invoking
// `bootstrap.SeedAutoNumberCounters` — passing a seed list with empty
// LegacySource silently disables legacy counter migration.
func BootstrapSeeds() []bootstrap.AutoNumberFormatSeed {
	// Standard platform seeds only. Tenant-specific autonumber formats
	// (compound formats that dereference tenant RecordType codes,
	// autonumbers on tenant `__c` objects) are applied by
	// `cmd/setup apply-tenant` against
	// controlplane/tenants/<tenant>/autonumber_formats.go. Platform
	// binary ships zero tenant identifiers (issue #1609).
	out := make([]bootstrap.AutoNumberFormatSeed, 0, len(AutoNumberFormatSeeds))
	for _, s := range AutoNumberFormatSeeds {
		out = append(out, bootstrap.AutoNumberFormatSeed{
			ObjectName:   s.ObjectName,
			FieldApiName: s.FieldApiName,
			Format:       s.Format,
			Scope:        s.Scope,
			Start:        s.Start,
			LegacySource: s.LegacySource,
		})
	}
	return out
}

// init registers the declared (ObjectName, FieldApiName) seed pairs with
// the bootstrap verifier so V8 can compute its declared-vs-reachable
// expected count (issue #1632). Without registration V8 falls back to
// the legacy fixed-floor `MinExpectedAutoNumberFieldsV8`, which
// false-positives on orgs that do not have every ISV managed-package
// installed (production often seeds ~35 of 132 declared targets).
//
// This init runs whenever controlplane/packs is linked in (every
// production binary: cmd/setup, cmd/server; every CI test that imports
// packs). Unit tests in bootstrap that do not depend on packs continue
// to hit the fixed-floor fallback — matching their existing expectation.
func init() {
	keys := make([]bootstrap.AutoNumberDeclaredSeedKey, 0, len(AutoNumberFormatSeeds))
	for _, s := range AutoNumberFormatSeeds {
		keys = append(keys, bootstrap.AutoNumberDeclaredSeedKey{
			ObjectName:   s.ObjectName,
			FieldApiName: s.FieldApiName,
		})
	}
	bootstrap.RegisterAutoNumberDeclaredSeeds(keys)
}
