package click.kamil.springstatemachineexporter.analysis.enricher.routing;

import click.kamil.springstatemachineexporter.analysis.model.CallChain;

/**
 * Decides whether a call chain is provably shared infrastructure that may intentionally
 * attach to multiple state machines when they share the same transition event.
 * <p>
 * Shared infrastructure requires source-derived evidence only: no distinct machine enum types
 * at the sendEvent site and no vertical domain ownership in the call chain classes/packages.
 * Event-name matching alone is never sufficient outside this policy.
 */
final class SharedServiceRoutingPolicy {

    private SharedServiceRoutingPolicy() {
    }

    /**
     * @return true when the chain may multi-attach to every machine that shares its transition event
     */
    static boolean isProvablySharedInfrastructure(CallChain chain) {
        if (chain == null) {
            return false;
        }
        MachineRoutingEvidence evidence = MachineRoutingEvidence.from(chain);
        if (evidence.eventTypeFqn() != null || evidence.stateTypeFqn() != null) {
            return false;
        }
        return !PackageNameRoutingHeuristics.hasVerticalDomainOwnership(chain);
    }
}
