package click.kamil.springstatemachineexporter.analysis.resolver;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

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

class PropertyResolverTest {

    @Test
    void testYamlPropertiesAreLoadedAndFlattened(@TempDir Path tempDir) throws IOException {
        Path applicationYml = tempDir.resolve("application.yml");
        Files.writeString(applicationYml, """
                messaging:
                  sqs:
                    integration-system1:
                      callback:
                        queue: "integration-system1-queue"
                """);

        PropertyResolver resolver = new PropertyResolver();
        Map<String, String> props = resolver.resolveProperties(tempDir);

        assertThat(props).containsEntry("messaging.sqs.integration-system1.callback.queue", "integration-system1-queue");
    }

    @Test
    void testPropertiesFilesAreLoaded(@TempDir Path tempDir) throws IOException {
        Path applicationProps = tempDir.resolve("application.properties");
        Files.writeString(applicationProps, "messaging.sqs.integration-system1.callback.queue=integration-system1-queue-props");

        PropertyResolver resolver = new PropertyResolver();
        Map<String, String> props = resolver.resolveProperties(tempDir);

        assertThat(props).containsEntry("messaging.sqs.integration-system1.callback.queue", "integration-system1-queue-props");
    }

    @Test
    void testPlaceholderResolution() {
        PropertyResolver resolver = new PropertyResolver();
        Map<String, String> properties = Map.of("messaging.sqs.integration-system1.callback.queue", "integration-system1-queue");

        String resolved = resolver.resolveValue("SQS: ${messaging.sqs.integration-system1.callback.queue}", properties);
        assertThat(resolved).isEqualTo("SQS: integration-system1-queue");
    }

    @Test
    void testPlaceholderResolutionWithDefault() {
        PropertyResolver resolver = new PropertyResolver();
        Map<String, String> properties = Map.of();

        String resolved = resolver.resolveValue("SQS: ${messaging.sqs.queue:default-queue}", properties);
        assertThat(resolved).isEqualTo("SQS: default-queue");
    }
}
