package click.kamil.springstatemachineexporter.analysis.enricher;

import click.kamil.springstatemachineexporter.analysis.enricher.routing.BeanResolutionEngine;
import click.kamil.springstatemachineexporter.analysis.enricher.routing.HeuristicBeanResolutionEngine;
import click.kamil.springstatemachineexporter.analysis.model.CallChain;
import click.kamil.springstatemachineexporter.analysis.model.EntryPoint;
import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import click.kamil.springstatemachineexporter.model.Transition;

import java.util.ArrayList;
import java.util.List;

/**
 * Filters codebase-wide analysis artifacts to those relevant for a single state machine config.
 */
public final class MachineScopeFilter {

    private static final BeanResolutionEngine ROUTING = new HeuristicBeanResolutionEngine();

    private MachineScopeFilter() {
    }

    public static List<TriggerPoint> filterTriggersForMachine(
            List<TriggerPoint> triggers, String machineName, CodebaseContext context) {
        return filterTriggersForMachine(triggers, machineName, context, null);
    }

    public static List<TriggerPoint> filterTriggersForMachine(
            List<TriggerPoint> triggers,
            String machineName,
            CodebaseContext context,
            List<Transition> machineTransitions) {
        if (triggers == null || machineName == null) {
            return triggers == null ? List.of() : triggers;
        }
        List<TriggerPoint> filtered = new ArrayList<>();
        for (TriggerPoint trigger : triggers) {
            if (isTriggerForMachine(trigger, machineName, context, machineTransitions)) {
                filtered.add(trigger);
            }
        }
        return filtered;
    }

    public static List<CallChain> filterCallChainsForMachine(
            List<CallChain> chains, String machineName, CodebaseContext context) {
        return filterCallChainsForMachine(chains, machineName, context, null);
    }

    public static List<CallChain> filterCallChainsForMachine(
            List<CallChain> chains,
            String machineName,
            CodebaseContext context,
            List<Transition> machineTransitions) {
        if (chains == null || machineName == null) {
            return chains == null ? List.of() : chains;
        }
        List<CallChain> filtered = new ArrayList<>();
        for (CallChain chain : chains) {
            if (ROUTING.hasProvenMachineAffinity(chain, machineName, context, machineTransitions)) {
                filtered.add(chain);
            }
        }
        return filtered;
    }

    public static List<EntryPoint> filterEntryPointsForMachine(
            List<EntryPoint> entryPoints, String machineName, CodebaseContext context) {
        if (entryPoints == null || machineName == null) {
            return entryPoints == null ? List.of() : entryPoints;
        }
        List<EntryPoint> filtered = new ArrayList<>();
        for (EntryPoint entryPoint : entryPoints) {
            if (isEntryPointForMachine(entryPoint, machineName, context)) {
                filtered.add(entryPoint);
            }
        }
        return filtered;
    }

    private static boolean isEntryPointForMachine(
            EntryPoint entryPoint, String machineName, CodebaseContext context) {
        if (entryPoint == null) {
            return false;
        }
        if (EntryPointScopeResolver.hasPathVariablePlaceholder(entryPoint)) {
            EntryPointScopeResolver.Affinity affinity =
                    EntryPointScopeResolver.resolveAffinity(entryPoint, machineName, context);
            return affinity != EntryPointScopeResolver.Affinity.AGAINST_MACHINE;
        }

        EntryPointScopeResolver.Affinity affinity =
                EntryPointScopeResolver.resolveAffinity(entryPoint, machineName, context);
        if (affinity == EntryPointScopeResolver.Affinity.FOR_MACHINE) {
            return true;
        }
        if (affinity == EntryPointScopeResolver.Affinity.AGAINST_MACHINE) {
            return false;
        }
        return false;
    }

    private static boolean isTriggerForMachine(
            TriggerPoint trigger, String machineName, CodebaseContext context, List<Transition> machineTransitions) {
        if (trigger == null) {
            return false;
        }
        CallChain probe = CallChain.builder().triggerPoint(trigger).methodChain(List.of()).build();
        return ROUTING.hasProvenMachineAffinity(probe, machineName, context, machineTransitions);
    }
}
