package click.kamil.springstatemachineexporter.analysis.service;

import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
import click.kamil.springstatemachineexporter.analysis.resolver.ConstantResolver;
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import org.eclipse.jdt.core.dom.CompilationUnit;
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;

class GenericEventDetectorRecordTest {

    @Test
    void shouldDetectSendEventInsideTopLevelRecord(@TempDir Path tempDir) throws Exception {
        Files.writeString(tempDir.resolve("EventBridge.java"), """
                package com.example;
                import org.springframework.statemachine.StateMachine;
                public record EventBridge(StateMachine<String, String> sm, String event) {
                    public void fire() {
                        sm.sendEvent(event);
                    }
                }
                """);

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

        GenericEventDetector detector = new GenericEventDetector(context, new ConstantResolver(), List.of());
        CompilationUnit cu = context.getCompilationUnits().iterator().next();
        List<TriggerPoint> triggers = detector.detect(cu);

        assertThat(triggers).hasSize(1);
        assertThat(triggers.get(0).getClassName()).isEqualTo("com.example.EventBridge");
        assertThat(triggers.get(0).getMethodName()).isEqualTo("fire");
        assertThat(triggers.get(0).getEvent()).isEqualTo("event");
    }
}
