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

    @TempDir
    Path tempDir;

    @Test
    void shouldResolveEventFromArrayAccess() throws Exception {
        String source = """
            package com.example;
            public class ArrayService {
                private EventService service;
                
                public void process() {
                    String[] events = new String[] {"EVENT_1", "EVENT_2"};
                    service.trigger(events[0]);
                }
            }
            class EventService {
                public void trigger(String ev) {}
            }
            """;
            
        Files.writeString(tempDir.resolve("ArrayService.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("ArrayService.java")
            .sourceModule("com.example")
            .event("events[0]")
            .lineNumber(7)
            .build();
            
        TriggerPoint result = engine.resolveTriggerPointParameters(trigger, List.of("com.example.ArrayService.process", "com.example.EventService.trigger"), java.util.Collections.emptyMap());
        
        // When using array indexing, we should ideally extract the indexed element or at least all elements in the array
        assertThat(result.getPolymorphicEvents()).contains("EVENT_1");
    }
}
