package click.kamil.springstatemachineexporter.analysis.resolver;

import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import org.junit.jupiter.api.Test;

import java.nio.file.Files;
import java.nio.file.Path;

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

class StateMachineTypeResolverEnterpriseTest {

    @Test
    void shouldResolveConcreteTypesThroughGenericAbstractBase() throws Exception {
        Path root = findProjectRoot();
        Path enterprise = root.resolve("state_machines/state_machine_enterprise");
        CodebaseContext context = new CodebaseContext();
        context.scan(enterprise);

        StateMachineTypeResolver.MachineTypes types = StateMachineTypeResolver.resolveTypes(
                "click.kamil.enterprise.machines.order.OrderStateMachineConfiguration", context);

        assertThat(types.stateTypeFqn())
                .isEqualTo("click.kamil.enterprise.machines.order.OrderState");
        assertThat(types.eventTypeFqn())
                .isEqualTo("click.kamil.enterprise.machines.order.OrderEvent");
        assertThat(StateMachineTypeResolver.extendsEnumStateMachineConfigurer(
                "click.kamil.enterprise.machines.order.OrderStateMachineConfiguration", context))
                .isTrue();
    }

    private static Path findProjectRoot() {
        Path current = Path.of(".").toAbsolutePath();
        while (current != null && !Files.exists(current.resolve("settings.gradle"))) {
            current = current.getParent();
        }
        return current;
    }
}
