package click.kamil.springstatemachineexporter.analysis.service;

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 org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class NewBugTest {

    @Test
    void shouldResolveMapLookup() throws IOException {
        String source = """
                package com.example;
                import java.util.Map;
                
                public class Controller {
                    private StateMachine machine;
                    private static final Map<InputEnum, TransitionEnum> MAP = Map.of(
                        InputEnum.IN_A, TransitionEnum.STATE_X,
                        InputEnum.IN_B, TransitionEnum.STATE_Y
                    );
                
                    public void process(InputEnum input) {
                        TransitionEnum trans = MAP.get(input);
                        machine.fire(trans);
                    }
                }
                
                class StateMachine {
                    public void fire(TransitionEnum event) {}
                }
                
                enum InputEnum { IN_A, IN_B }
                enum TransitionEnum { STATE_X, STATE_Y }
                """;

        Path tempDir = Files.createTempDirectory("callgraph_test_map");
        Files.writeString(tempDir.resolve("Config.java"), source);

        CodebaseContext context = new CodebaseContext();
        context.scan(tempDir);

        HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);

        EntryPoint entryPoint = EntryPoint.builder()
                .className("com.example.Controller")
                .methodName("process")
                .build();

        TriggerPoint trigger = TriggerPoint.builder()
                .className("com.example.StateMachine")
                .methodName("fire")
                .event("event")
                .build();

        List<CallChain> chains = engine.findChains(List.of(entryPoint), List.of(trigger));
        assertThat(chains).hasSize(1);
        System.out.println("Poly events: " + chains.get(0).getTriggerPoint().getPolymorphicEvents());
        assertThat(chains.get(0).getTriggerPoint().getPolymorphicEvents()).contains("TransitionEnum.STATE_X", "TransitionEnum.STATE_Y");
    }
}
