package click.kamil.springstatemachineexporter.analysis.enricher;

import click.kamil.springstatemachineexporter.analysis.model.AnalysisResult;
import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
import click.kamil.springstatemachineexporter.analysis.resolver.MachineEnumCanonicalizer;
import click.kamil.springstatemachineexporter.analysis.resolver.StateMachineTypeResolver;
import click.kamil.springstatemachineexporter.analysis.service.CodebaseIntelligenceProvider;
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class TriggerEnricher implements AnalysisEnricher {

    @Override
    public void enrich(AnalysisResult result, CodebaseContext context, CodebaseIntelligenceProvider intelligence) {
        log.info("Enriching {} with triggers", result.getName());

        StateMachineTypeResolver.MachineTypes machineTypes =
                StateMachineTypeResolver.resolveTypes(result.getName(), context);

        List<TriggerPoint> triggers = intelligence.findTriggerPoints().stream()
                .map(trigger -> MachineEnumCanonicalizer.canonicalizeTriggerPoint(trigger, machineTypes))
                .collect(Collectors.toList());
        triggers = MachineScopeFilter.filterTriggersForMachine(triggers, result.getName(), context);

        result.addMetadata(click.kamil.springstatemachineexporter.analysis.model.CodebaseMetadata.builder()
                .triggers(triggers)
                .build());
    }
}
