package bootstrap

import (
	"database/sql"

	cruntimeports "github.com/nexuscrm/db/pkg/runtimeports"
	seedrows "github.com/pingcap-inc/nexuscrm-contract/pkg/seedrows"
)

// StandardActionReaderImpl is the concrete cruntimeports.StandardActionReader.
// It binds a *sql.DB at construction and delegates each method to the canonical
// bootstrap reader function of the same name. It carries no logic of its own:
// behavior is byte-for-byte identical to calling the bootstrap function directly.
//
// The port interface lives in the contract module's pkg/runtimeports (a leaf
// package that imports nothing backend-internal). bootstrap is allowed to import
// it, so the concrete impl is hosted here next to the functions it wraps. The
// composition root (internal/application/services) constructs this and injects it
// into the standard-action service, which depends only on the port.
type StandardActionReaderImpl struct {
	db *sql.DB
}

// NewStandardActionReaderImpl returns a StandardActionReaderImpl backed by rawDB.
// Intended for composition-root wiring.
func NewStandardActionReaderImpl(rawDB *sql.DB) StandardActionReaderImpl {
	return StandardActionReaderImpl{db: rawDB}
}

// Compile-time assertion that the bootstrap impl satisfies the contract port.
var _ cruntimeports.StandardActionReader = StandardActionReaderImpl{}

// GetActionSchema delegates to bootstrap.GetActionSchema with the bound DB.
func (r StandardActionReaderImpl) GetActionSchema(db *sql.DB, standardActionID string) ([]seedrows.ActionSchemaRow, error) {
	return GetActionSchema(db, standardActionID)
}

// GetActionSchemas delegates to bootstrap.GetActionSchemas with the bound DB.
func (r StandardActionReaderImpl) GetActionSchemas(db *sql.DB, standardActionIDs []string) (map[string][]seedrows.ActionSchemaRow, error) {
	return GetActionSchemas(db, standardActionIDs)
}

// GetActionFieldDefaults delegates to bootstrap.GetActionFieldDefaults with the bound DB.
func (r StandardActionReaderImpl) GetActionFieldDefaults(db *sql.DB, standardActionID string) ([]seedrows.ActionFieldDefaultRow, error) {
	return GetActionFieldDefaults(db, standardActionID)
}

// GetActionFieldDefaultsForActions delegates to
// bootstrap.GetActionFieldDefaultsForActions with the bound DB.
func (r StandardActionReaderImpl) GetActionFieldDefaultsForActions(db *sql.DB, standardActionIDs []string) (map[string][]seedrows.ActionFieldDefaultRow, error) {
	return GetActionFieldDefaultsForActions(db, standardActionIDs)
}

// GetActionInputGuards delegates to bootstrap.GetActionInputGuards with the bound DB.
func (r StandardActionReaderImpl) GetActionInputGuards(db *sql.DB, standardActionID string) ([]seedrows.ActionInputGuardRow, error) {
	return GetActionInputGuards(db, standardActionID)
}

// GetActionInputGuardsForActions delegates to
// bootstrap.GetActionInputGuardsForActions with the bound DB.
func (r StandardActionReaderImpl) GetActionInputGuardsForActions(db *sql.DB, standardActionIDs []string) (map[string][]seedrows.ActionInputGuardRow, error) {
	return GetActionInputGuardsForActions(db, standardActionIDs)
}

// GetStandardActionInternalAccessScopes delegates to
// bootstrap.GetStandardActionInternalAccessScopes with the bound DB.
func (r StandardActionReaderImpl) GetStandardActionInternalAccessScopes(db *sql.DB, standardActionID string) ([]seedrows.StandardActionInternalAccessScopeRow, error) {
	return GetStandardActionInternalAccessScopes(db, standardActionID)
}

// GetFieldMappingSourceForTarget delegates to
// bootstrap.GetFieldMappingSourceForTarget with the bound DB.
func (r StandardActionReaderImpl) GetFieldMappingSourceForTarget(db *sql.DB, sourceObject, targetObject, actionType, targetField string) (string, error) {
	return GetFieldMappingSourceForTarget(db, sourceObject, targetObject, actionType, targetField)
}
