package click.kamil.springstatemachineexporter.analysis.service;

import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;

public class StreamApiTests {

    @TempDir
    Path tempDir;

    @Test
    void shouldHandleStreamApiChains() throws Exception {
        String source = """
            package com.example;
            import java.util.List;
            
            public class StreamService {
                private EventService service;
                
                public void process(List<MyEvent> events) {
                    events.stream()
                        .filter(e -> e.isValid())
                        .map(e -> e.toEvent())
                        .forEach(t -> service.trigger(t.getEvent()));
                }
            }
            class EventService {
                public void trigger(String ev) {}
            }
            class MyEvent {
                public boolean isValid() { return true; }
                public OrderEvent toEvent() { return new OrderEvent(); }
            }
            class OrderEvent {
                public String getEvent() { return "STREAM_EVENT"; }
            }
            """;
            
        Files.writeString(tempDir.resolve("StreamService.java"), source);
        CodebaseContext context = new CodebaseContext();
        context.scan(tempDir);

        HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
        
        TriggerPoint trigger = TriggerPoint.builder()
            .className("com.example.EventService")
            .methodName("trigger")
            .sourceFile("StreamService.java")
            .sourceModule("com.example")
            .event("t.getEvent()")
            .lineNumber(11)
            .build();
            
        TriggerPoint result = engine.resolveTriggerPointParameters(trigger, List.of("com.example.StreamService.process", "com.example.EventService.trigger"), java.util.Collections.emptyMap());
        System.out.println("TEST RESULT: " + result);
        System.out.println("POLY EVENTS: " + result.getPolymorphicEvents());
        
        assertThat(result.getPolymorphicEvents()).isNotNull().containsExactlyInAnyOrder("STREAM_EVENT");
    }
}
