package click.kamil.springstatemachineexporter.analysis.service;

import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
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;

/**
 * Export must work on standard Spring State Machine patterns without hints.json.
 */
class ExportWithoutHintsTest {

    @Test
    void shouldDetectSendEventTriggersWithoutHintsJson(@TempDir Path tempDir) throws Exception {
        String source = """
            package com.example;
            import org.springframework.statemachine.StateMachine;
            public class OrderService {
                public void pay(StateMachine<OrderState, OrderEvent> sm) {
                    sm.sendEvent(OrderEvent.PAY);
                }
            }
            enum OrderState { NEW, PAID }
            enum OrderEvent { PAY }
            """;
        Files.writeString(tempDir.resolve("OrderService.java"), source);
        assertThat(tempDir.resolve("hints.json")).doesNotExist();

        CodebaseContext context = new CodebaseContext();
        context.scan(tempDir);
        assertThat(context.getLibraryHints()).isEmpty();

        GenericEventDetector detector = new GenericEventDetector(
                context, context.getConstantResolver(), context.getLibraryHints());
        List<TriggerPoint> triggers = detector.detect(
                context.getCompilationUnits().iterator().next());

        assertThat(triggers).isNotEmpty();
        assertThat(triggers)
                .extracting(TriggerPoint::getEvent)
                .anyMatch(e -> e != null && e.contains("PAY"));
    }
}
