package click.kamil.springstatemachineexporter.analysis;

import click.kamil.springstatemachineexporter.analysis.resolver.ConstantResolver;
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
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.Set;

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

public class MultiModuleResolutionTest {

    @Test
    void testResolutionAcrossModules(@TempDir Path tempDir) throws IOException {
        // Module A
        Path moduleADir = tempDir.resolve("module-a");
        Path srcA = moduleADir.resolve("src/main/java/com/modulea");
        Files.createDirectories(srcA);
        Files.writeString(srcA.resolve("CommonConstants.java"),
            "package com.modulea;\n" +
            "public class CommonConstants {\n" +
            "    public static final String API_BASE = \"/api/v1\";\n" +
            "}");

        // Module B
        Path moduleBDir = tempDir.resolve("module-b");
        Path srcB = moduleBDir.resolve("src/main/java/com/moduleb");
        Files.createDirectories(srcB);
        Files.writeString(srcB.resolve("SpecificConstants.java"),
            "package com.moduleb;\n" +
            "import com.modulea.CommonConstants;\n" +
            "public class SpecificConstants {\n" +
            "    public static final String USERS = CommonConstants.API_BASE + \"/users\";\n" +
            "}");

        CodebaseContext context = new CodebaseContext();
        // Scan both module roots
        context.scan(Set.of(moduleADir, moduleBDir), Set.of());

        TypeDeclaration td = context.getTypeDeclaration("com.moduleb.SpecificConstants");
        FieldDeclaration field = td.getFields()[0];
        VariableDeclarationFragment fragment = (VariableDeclarationFragment) field.fragments().get(0);

        ConstantResolver resolver = new ConstantResolver();
        String result = resolver.resolve(fragment.getInitializer(), context);

        assertThat(result).isEqualTo("/api/v1/users");
    }
}
