// Package seamtestwiring is the control-plane test composition root. It injects
// the canonical seam implementations into the control-plane seam ports for the
// CP/packs/tenantoverlay test binaries, WITHOUT importing backend.
//
// As of PR2 the heavy schema/field-provenance/autonumber impls live in the
// public package github.com/pingcap-inc/nexuscrm-controlplane/bootstrapimpl, and the
// bootstrap-state store + db-passing reader functions live in
// github.com/nexuscrm/db. Wiring directly from those neutral homes lets the CP
// test TestMains exercise the real apply/verify/reapply seams with no
// controlplane->backend edge (the dependency PR2 severs). It deliberately does
// NOT go through backend/pkg/serverruntime.WireControlplaneSeams: importing
// serverruntime (which imports controlplane) would re-introduce a
// controlplane->backend edge inside a CP test binary.
//
// This package is internal to the controlplane module, so only CP test files can
// import it. The wiring is byte-for-byte identical to the production
// WireControlplaneSeams entrypoint.
package seamtestwiring

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"
)

// Wire injects the control-plane seam ports from bootstrapimpl + db. It is
// idempotent (the impls are stateless value types, the readers are pure free
// functions, and the setters nil-guard), so repeated calls are safe.
func Wire() {
	wireServiceGraph()

	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,
	// after the seeder port is injected (mirrors the production wiring). For L0
	// AutoNumberFormatSeeds is empty, so this is a no-op today.
	packs.RegisterPlatformDeclaredAutoNumberSeeds()

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

	controlplane.SetGetActionSchema(connectorreaders.GetActionSchema)

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