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

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

/**
 * Source-derived evidence used to decide whether a call chain belongs to a state-machine export.
 */
public record MachineRoutingEvidence(
        String explicitBeanName,
        String stateMachineId,
        String eventTypeFqn,
        String stateTypeFqn,
        String triggerClassFqn,
        String triggerMethod,
        Integer triggerLine) {

    public static MachineRoutingEvidence from(CallChain chain) {
        if (chain == null) {
            return new MachineRoutingEvidence(null, null, null, null, null, null, null);
        }
        TriggerPoint trigger = chain.getTriggerPoint();
        String explicit = chain.getContextMachineId();
        String stateMachineId = null;
        Integer line = null;
        if (trigger != null) {
            if (explicit == null) {
                explicit = trigger.getStateMachineId();
            }
            stateMachineId = trigger.getStateMachineId();
            if (trigger.getLineNumber() > 0) {
                line = trigger.getLineNumber();
            }
        }
        return new MachineRoutingEvidence(
                explicit,
                stateMachineId,
                trigger != null ? trigger.getEventTypeFqn() : null,
                trigger != null ? trigger.getStateTypeFqn() : null,
                trigger != null ? trigger.getClassName() : null,
                trigger != null ? trigger.getMethodName() : null,
                line);
    }
}
