package click.kamil.springstatemachineexporter.analysis.service;

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

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

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

@Tag("dynamic_classpath_resolver")
class DynamicClasspathResolverTest {

    @Test
    void shouldResolveMavenClasspathDynamically(@TempDir Path tempDir) throws IOException {
        // Create dummy pom.xml
        Files.createFile(tempDir.resolve("pom.xml"));
        
        // Mock the resolver so it doesn't actually run maven, but writes the expected output file
        DynamicClasspathResolver resolver = new DynamicClasspathResolver() {
            @Override
            protected void runProcess(ProcessBuilder pb) throws IOException {
                // Pretend maven executed and generated the cp.txt
                String cp = "/fake/m2/repo/spring-core.jar" + File.pathSeparator + "/fake/m2/repo/spring-context.jar";
                
                String outputFileArg = pb.command().stream()
                        .filter(arg -> arg.startsWith("-Dmdep.outputFile="))
                        .findFirst()
                        .orElseThrow();
                Path outputFile = pb.directory().toPath().resolve(outputFileArg.substring("-Dmdep.outputFile=".length()));
                
                Files.writeString(outputFile, cp);
            }
        };

        List<String> classpath = resolver.resolveClasspath(tempDir);
        
        assertThat(classpath).containsExactly(
                "/fake/m2/repo/spring-core.jar",
                "/fake/m2/repo/spring-context.jar"
        );
    }

    @Test
    void shouldResolveGradleClasspathDynamically(@TempDir Path tempDir) throws IOException {
        // Create dummy build.gradle
        Files.createFile(tempDir.resolve("build.gradle"));
        
        DynamicClasspathResolver resolver = new DynamicClasspathResolver() {
            @Override
            protected String runProcessAndGetOutput(ProcessBuilder pb) {
                // Pretend gradle executed and printed the marker
                String cp = "/fake/gradle/caches/spring-core.jar" + File.pathSeparator + "/fake/gradle/caches/spring-context.jar";
                return "Some noisy gradle output\n" +
                       "SME_CLASSPATH_MARKER:" + cp + "\n" +
                       "BUILD SUCCESSFUL\n";
            }
        };

        List<String> classpath = resolver.resolveClasspath(tempDir);
        
        assertThat(classpath).containsExactly(
                "/fake/gradle/caches/spring-core.jar",
                "/fake/gradle/caches/spring-context.jar"
        );
    }

    @Test
    void shouldReturnEmptyListIfNoBuildFileFound(@TempDir Path tempDir) {
        DynamicClasspathResolver resolver = new DynamicClasspathResolver();
        List<String> classpath = resolver.resolveClasspath(tempDir);
        assertThat(classpath).isEmpty();
    }
}
