package click.kamil.springstatemachineexporter.service;

import click.kamil.springstatemachineexporter.analysis.model.AnalysisResult;
import click.kamil.springstatemachineexporter.model.State;
import click.kamil.springstatemachineexporter.model.Transition;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

public class DeferredProfileResolutionTest {

    @Test
    void shouldResolvePlaceholdersWithDefaultProfile() {
        // Given
        AnalysisResult result = createMockResult();
        Map<String, String> properties = Map.of("app.state.initial", "START_RESOLVED");

        // When
        result.applyResolution(properties);

        // Then
        assertThat(result.getStartStates()).containsExactly("START_RESOLVED");
        
        Transition t = result.getTransitions().get(0);
        assertThat(t.getEvent().fullIdentifier()).isEqualTo("RESOLVED_EVENT");
        assertThat(t.getSourceStates().get(0).fullIdentifier()).isEqualTo("START_RESOLVED");
    }

    @Test
    void shouldResolveWithFallback() {
        // Given
        AnalysisResult result = AnalysisResult.builder()
                .name("TestSM")
                .startStates(Set.of("${missing.key:FALLBACK}"))
                .transitions(Collections.emptyList())
                .build();
        
        // When
        result.applyResolution(Collections.emptyMap());

        // Then
        assertThat(result.getStartStates()).containsExactly("FALLBACK");
    }

    private AnalysisResult createMockResult() {
        String placeholder = "${app.state.initial}";
        State s1 = State.of("START", placeholder);
        State s2 = State.of("END", "END");

        Transition t1 = new Transition();
        t1.setEvent(click.kamil.springstatemachineexporter.model.Event.of("${app.event.name:RESOLVED_EVENT}"));
        t1.setSourceStates(List.of(s1));
        t1.setTargetStates(List.of(s2));

        return AnalysisResult.builder()
                .name("MockSM")
                .startStates(Set.of(placeholder))
                .endStates(Set.of("END"))
                .states(Set.of(s1, s2))
                .transitions(List.of(t1))
                .build();
    }
}
