package serverruntime

import (
	"github.com/nexuscrm/db/pkg/bootstate"
	connectorreaders "github.com/nexuscrm/db/pkg/connectorreaders"
	"github.com/pingcap-inc/nexuscrm-controlplane"
	"github.com/pingcap-inc/nexuscrm-controlplane/bootstrapimpl"
	"github.com/pingcap-inc/nexuscrm-controlplane/packs"
	"github.com/pingcap-inc/nexuscrm-controlplane/tenantoverlay"
)

// WireControlplaneSeams injects the canonical seam implementations and reader
// functions into the control-plane seam ports.
//
// As of PR2 the control-plane packages (internal/controlplane, .../packs,
// .../tenantoverlay) no longer import internal/bootstrap at all: every seam var
// is declared nil and the concrete implementation is owned by one of two neutral
// homes that controlplane is allowed to depend on —
//   - the heavy schema/field-provenance/autonumber impls live in the public
//     package github.com/pingcap-inc/nexuscrm-controlplane/bootstrapimpl, and
//   - the bootstrap-state store and the db-passing reader functions live in
//     github.com/nexuscrm/db.
//
// This function is the single backend-side wiring entrypoint; every entry point
// that runs the control-plane apply/verify/reapply or tenant-overlay path MUST
// call it before invoking that path, or the seam ports are nil.
//
// It is invoked from:
//   - internal/setupcmd (cmd/setup, all apply/verify subcommands),
//   - control-plane test wiring (the CP/packs/tenantoverlay test TestMains wire
//     the same impls directly from bootstrapimpl + db, because importing this
//     package — which imports controlplane — would form an import cycle in a CP
//     test binary).
//
// The impls are stateless value types and the reader functions are pure free
// functions, so wiring is idempotent and behavior is byte-for-byte identical to
// the prior in-CP default construction. The nil-guarding setters make repeated
// calls safe.
func WireControlplaneSeams() {
	// Stateless *Impl port seams.
	schemaProvisioner := bootstrapimpl.SchemaProvisionerImpl{}
	fieldProvenanceInitializer := bootstrapimpl.FieldProvenanceInitializerImpl{}
	autoNumberSeeder := bootstrapimpl.AutoNumberSeederImpl{}
	bootstrapStateStore := bootstate.BootstrapStateStoreImpl{}

	controlplane.SetBootstrapStateStore(bootstrapStateStore)
	controlplane.SetFieldProvenanceInitializer(fieldProvenanceInitializer)
	controlplane.SetSchemaProvisioner(schemaProvisioner)

	packs.SetFieldProvenanceInitializer(fieldProvenanceInitializer)
	packs.SetSchemaProvisioner(schemaProvisioner)
	packs.SetAutoNumberSeeder(autoNumberSeeder)
	// Register the platform-declared autonumber seed keys with the verifier.
	// This was previously a packs init() that depended on the seam's
	// bootstrap-backed default; with the default removed it runs here, after the
	// seeder port is injected. AutoNumberFormatSeeds is empty for L0, so this is a
	// no-op today and behavior is identical to the prior init.
	packs.RegisterPlatformDeclaredAutoNumberSeeds()

	tenantoverlay.SetAutoNumberSeeder(autoNumberSeeder)
	tenantoverlay.SetSchemaProvisioner(schemaProvisioner)

	// db-passing reader function seams (shared with the automation feature via
	// the contract runtimeports ports; only the CP write/seed side relocated).
	controlplane.SetGetActionSchema(connectorreaders.GetActionSchema)

	packs.SetResolveFieldMapping(connectorreaders.ResolveFieldMapping)
	packs.SetResolvePackConnectorFieldMapping(connectorreaders.ResolvePackConnectorFieldMapping)
	packs.SetGetFieldMappings(connectorreaders.GetFieldMappings)
	packs.SetStandardFlowActiveVersionIsPackOwned(connectorreaders.StandardFlowActiveVersionIsPackOwned)
}
