package click.kamil.springstatemachineexporter.exporter;

import click.kamil.springstatemachineexporter.model.State;
import click.kamil.springstatemachineexporter.model.Transition;
import click.kamil.springstatemachineexporter.model.TransitionType;
import click.kamil.springstatemachineexporter.model.Guard;
import click.kamil.springstatemachineexporter.model.Action;
import org.junit.jupiter.api.Test;

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

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

class PlantUmlTest {

    @Test
    void testExportSanitizesLabels() {
        PlantUml plantUml = new PlantUml();
        ExportOptions options = ExportOptions.builder()
                .embedIdentifiers(true)
                .useLambdaGuards(false)
                .build();

        State state1 = State.of("State1");
        State state2 = State.of("State2");

        Transition t = new Transition();
        t.setType(TransitionType.EXTERNAL);
        t.setSourceStates(List.of(state1));
        t.setTargetStates(List.of(state2));
        
        // Add a guard and action containing problematic characters
        t.getGuards().add(Guard.of("list.size() < 5", false, null, null, null, null));
        t.setActions(List.of(Action.of("doSomething(new int[]{1, 2})", false, null, null, null, null)));

        String result = plantUml.export(
                "TestMachine",
                List.of(t),
                Set.of("State1"),
                Set.of("State2"),
                options
        );

        // Verify that < and > are replaced with HTML entities
        assertThat(result).doesNotContain("< 5");
        assertThat(result).contains("&lt; 5");

        // Verify that [ and ] are replaced with HTML entities inside the link
        assertThat(result).doesNotContain("[list.size() &lt; 5]");
        assertThat(result).doesNotContain("new int[]{1, 2}");
        assertThat(result).contains("&#91;list.size() &lt; 5&#93;");
        assertThat(result).contains("new int&#91;&#93;{1, 2}");
    }

    @Test
    void shouldFormatPackageCanonicalStartAndEndStatesLikeTransitionNodes() {
        PlantUml plantUml = new PlantUml();
        ExportOptions options = ExportOptions.builder().build();

        State stateA = State.of("State.A", "com.abcd.aaa.State.A");
        State stateB = State.of("State.B", "com.abcd.aaa.State.B");

        Transition t = new Transition();
        t.setType(TransitionType.EXTERNAL);
        t.setSourceStates(List.of(stateA));
        t.setTargetStates(List.of(stateB));

        String result = plantUml.export(
                "TestMachine",
                List.of(t),
                Set.of("com.abcd.aaa.State.A"),
                Set.of("com.abcd.aaa.State.B"),
                options);

        assertThat(result).contains("[*] --> State.A");
        assertThat(result).contains("State.B --> [*]");
        assertThat(result).doesNotContain("com.abcd.aaa.State.A");
        assertThat(result).doesNotContain("abcd.aaa.State.A");
    }
}
