// Package packs — bootstrap_shim.go exposes the platform
// AutoNumberFormatSeeds slice to callers in the bootstrap domain without
// creating a packs → bootstrap → packs import cycle (packs already
// imports bootstrap for ReconcileTableColumns / InitializeFieldMappings).
//
// The `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/pingcap-inc/nexuscrm-contract/pkg/autonumberseed"
)

// BootstrapSeeds returns every platform 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() []autonumberseed.AutoNumberFormatSeed {
	// Standard platform seeds only. CRM app-pack autonumber formats come
	// from app-pack payload roots; tenant-specific autonumber formats
	// (compound formats that dereference tenant RecordType codes,
	// autonumbers on tenant `__c` objects) are applied by
	// `cmd/setup apply-tenant` against external tenant overlay payload.
	// Platform binary ships zero tenant identifiers (issue #1609).
	out := make([]autonumberseed.AutoNumberFormatSeed, 0, len(AutoNumberFormatSeeds))
	for _, s := range AutoNumberFormatSeeds {
		out = append(out, autonumberseed.AutoNumberFormatSeed{
			ObjectName:   s.ObjectName,
			FieldApiName: s.FieldApiName,
			Format:       s.Format,
			Scope:        s.Scope,
			Start:        s.Start,
			LegacySource: s.LegacySource,
		})
	}
	return out
}

// platformDeclaredAutoNumberSeedKeys projects the platform-declared
// (ObjectName, FieldApiName) seed pairs that the bootstrap verifier needs so V8
// can compute its declared-vs-reachable expected count. L0 currently declares
// none (AutoNumberFormatSeeds is empty); source-backed app packs register their
// own seeds when applied or verified.
func platformDeclaredAutoNumberSeedKeys() []autonumberseed.AutoNumberDeclaredSeedKey {
	keys := make([]autonumberseed.AutoNumberDeclaredSeedKey, 0, len(AutoNumberFormatSeeds))
	for _, s := range AutoNumberFormatSeeds {
		keys = append(keys, autonumberseed.AutoNumberDeclaredSeedKey{
			ObjectName:   s.ObjectName,
			FieldApiName: s.FieldApiName,
		})
	}
	return keys
}

// RegisterPlatformDeclaredAutoNumberSeeds registers the platform-declared seed
// keys with the autonumber seed verifier through the injected seeder port.
//
// W5/S7: this was previously an init() that called the port at package-load
// time, which depended on the seam carrying a non-nil bootstrap-backed default.
// With the default removed (control-plane imports bootstrap = 0), the seeder is
// nil until a composition root injects it, so the registration is performed by
// the wiring entrypoint (serverruntime.WireControlplaneSeams, which every real
// entry point and the CP test wiring call) instead of at load time. The nil
// guard keeps it safe if ever called before wiring. AutoNumberFormatSeeds is
// empty for L0, so this is a no-op today and behavior is identical to the prior
// init; it stays correct if platform seeds are added later.
func RegisterPlatformDeclaredAutoNumberSeeds() {
	if autoNumberSeeder == nil {
		return
	}
	autoNumberSeeder.RegisterAutoNumberDeclaredSeeds(platformDeclaredAutoNumberSeedKeys())
}
