package click.kamil.springstatemachineexporter.html;

import click.kamil.springstatemachineexporter.analysis.model.AnalysisResult;
import click.kamil.springstatemachineexporter.analysis.model.CallChain;
import click.kamil.springstatemachineexporter.analysis.model.CodebaseMetadata;
import click.kamil.springstatemachineexporter.analysis.model.EntryPoint;
import click.kamil.springstatemachineexporter.analysis.model.TriggerPoint;
import click.kamil.springstatemachineexporter.exporter.ExportOptions;
import click.kamil.springstatemachineexporter.html.exporter.HtmlExporter;
import click.kamil.springstatemachineexporter.model.Event;
import click.kamil.springstatemachineexporter.model.State;
import click.kamil.springstatemachineexporter.model.Transition;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

class ExtendedStateMachineLifecycleHtmlTest {

    @Test
    void shouldShowLifecycleRestoreEndpointsInSidebar() {
        EntryPoint captureEndpoint = EntryPoint.builder()
                .type(EntryPoint.Type.REST)
                .name("POST /api/payment/{id}/capture")
                .className("click.kamil.examples.statemachine.extended.web.PaymentController")
                .methodName("capturePaymentEndpoint")
                .sourceFile("src/main/java/click/kamil/examples/statemachine/extended/web/PaymentController.java")
                .build();
        CallChain lifecycleChain = CallChain.builder()
                .entryPoint(captureEndpoint)
                .triggerPoint(TriggerPoint.builder()
                        .event("[LIFECYCLE:RESTORE]")
                        .className("click.kamil.examples.statemachine.extended.service.PaymentServiceImpl")
                        .methodName("processPayment")
                        .sourceFile("src/main/java/click/kamil/examples/statemachine/extended/service/PaymentServiceImpl.java")
                        .build())
                .build();

        Transition transition = new Transition();
        transition.setEvent(Event.of("CAPTURE", "CAPTURE"));
        transition.setSourceStates(List.of(State.of("INIT", "INIT")));
        transition.setTargetStates(List.of(State.of("CAPTURED", "CAPTURED")));

        AnalysisResult result = AnalysisResult.builder()
                .name("click.kamil.examples.statemachine.extended.config.ExtendedStateMachineConfig")
                .transitions(List.of(transition))
                .startStates(Set.of("INIT"))
                .endStates(Set.of("CAPTURED"))
                .metadata(CodebaseMetadata.builder()
                        .entryPoints(List.of(captureEndpoint))
                        .callChains(List.of(lifecycleChain))
                        .build())
                .build();

        String html = new HtmlExporter().export(result, ExportOptions.builder().includeMetadataPane(true).build());

        assertThat(html).contains("function formatLifecycleLabel");
        assertThat(html).contains("POST /api/payment/{id}/capture");
        assertThat(html).contains("Restore persisted state");
        assertThat(html).contains("[LIFECYCLE:RESTORE]");
        assertThat(html).contains("function isLifecycleChain(c)");
    }
}
