package controlplane

import (
	database "github.com/nexuscrm/db"
	"github.com/nexuscrm/db/pkg/runtimeports"
)

// CONTROL-PLANE SEAM — control-plane imports bootstrap = ZERO (W5/S7).
//
// schemaProvisioner is the control-plane's physical schema / DDL / reconcile
// port. The verify paths (verify.go, verify_l0.go) call this port instead of the
// internal/bootstrap functions directly, so the behavioral dependency is
// inverted: the concrete implementation is constructed and injected at the
// composition roots (pkg/serverruntime, internal/setupcmd, CP test wiring) via
// SetSchemaProvisioner — never defaulted here. The control-plane no longer
// references internal/bootstrap; the concrete impl is owned by the backend-side
// wiring entrypoint (serverruntime.WireControlplaneSeams).
//
// Mandatory injection: every entry point that runs the verify/apply path MUST
// wire this port before calling it, or it is nil.
var schemaProvisioner runtimeports.SchemaProvisioner

type connectedDeliverySchemaVerifierPort interface {
	VerifyConnectedDeliverySchema(*database.TiDBConnection) error
}

type podReadinessAssertionRunnerPort interface {
	RunPodReadinessAssertions(*database.TiDBConnection) error
}

var connectedDeliverySchemaVerifier connectedDeliverySchemaVerifierPort
var podReadinessAssertionRunner podReadinessAssertionRunnerPort

// SetSchemaProvisioner injects the control-plane schema-provisioner port.
// Intended for the composition roots (pkg/serverruntime via
// WireControlplaneSeams, internal/setupcmd, CP test wiring), which construct
// the bootstrap implementation and inject it here so the concrete dependency is
// owned by the composition root rather than the control-plane verify path.
func SetSchemaProvisioner(p runtimeports.SchemaProvisioner) {
	if p == nil {
		return
	}
	schemaProvisioner = p
	connectedDeliverySchemaVerifier, _ = p.(connectedDeliverySchemaVerifierPort)
	podReadinessAssertionRunner, _ = p.(podReadinessAssertionRunnerPort)
}
