package click.kamil.springstatemachineexporter;

import click.kamil.springstatemachineexporter.exporter.JsonExporter;
import click.kamil.springstatemachineexporter.service.ExportService;
import click.kamil.springstatemachineexporter.service.JsonImportService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;
import java.util.List;

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

class EnterpriseInitialStateExportTest {

    @Test
    void shouldExportCanonicalInitialStateForValuePlaceholder(@TempDir Path outputDir) throws Exception {
        Path inputDir = Path.of("../state_machines/enterprise_order_system").toAbsolutePath().normalize();

        ExportService exportService = new ExportService(List.of(new JsonExporter()));
        exportService.runExporter(inputDir, outputDir, List.of("json"), true, List.of());

        Path jsonFile = outputDir.resolve("click.kamil.examples.enterprise.config.EnterpriseStateMachineConfig")
                .resolve("click.kamil.examples.enterprise.config.EnterpriseStateMachineConfig.json");

        var result = new JsonImportService().importAnalysisResult(jsonFile);

        assertThat(result.getStateTypeFqn()).isEqualTo("String");
        assertThat(result.getStartStates()).containsExactly("String.NEW");
        assertThat(result.getStates())
                .anyMatch(s -> "String.NEW".equals(s.fullIdentifier())
                        && !s.rawName().contains("${"));
        assertThat(result.getStates())
                .noneMatch(s -> s.rawName().contains("${order.initial.state"));
    }
}
