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 AnonymousClassTests {

    @TempDir
    Path tempDir;

    @Test
    void shouldResolveEventFromAnonymousClass() throws Exception {
        String source = """
            package com.example;
            public class AnonymousService {
                private EventService service;
                
                public void process() {
                    MyEvent event = new MyEvent() {
                        @Override
                        public String getEvent() {
                            return "ANON_EVENT";
                        }
                    };
                    service.trigger(event.getEvent());
                }
            }
            class EventService {
                public void trigger(String ev) {}
            }
            class MyEvent {
                public String getEvent() { return "BASE_EVENT"; }
            }
            """;
            
        Files.writeString(tempDir.resolve("AnonymousService.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("AnonymousService.java")
            .sourceModule("com.example")
            .event("event.getEvent()")
            .lineNumber(12)
            .build();
            
        TriggerPoint result = engine.resolveTriggerPointParameters(trigger, List.of("com.example.AnonymousService.process", "com.example.EventService.trigger"), java.util.Collections.emptyMap());
        
        assertThat(result.getPolymorphicEvents()).contains("ANON_EVENT");
    }
}
