package click.kamil.springstatemachineexporter.analysis.service;

import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

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

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

class InheritanceCallTargetResolverTest {

    @Test
    void shouldResolveInheritedMethodOnDeclaringParentType(@TempDir Path tempDir) throws Exception {
        String source = """
            package com.example;
            public class Child extends Parent {
                void run() { dispatch(); }
            }
            class Parent {
                protected void dispatch() {}
            }
            """;
        Files.writeString(tempDir.resolve("App.java"), source);
        CodebaseContext context = new CodebaseContext();
        context.scan(tempDir);

        TypeDeclaration child = context.getTypeDeclaration("com.example.Child");
        MethodDeclaration run = context.findMethodDeclaration(child, "run", false);

        String target = InheritanceCallTargetResolver.resolveInstanceMethod(context, child, "dispatch");
        assertThat(target).isEqualTo("com.example.Parent.dispatch");
        assertThat(run).isNotNull();
    }

    @Test
    void shouldResolveSuperCallToParentMethod(@TempDir Path tempDir) throws Exception {
        String source = """
            package com.example;
            public class Child extends Parent {
                void run() { super.dispatch(); }
            }
            class Parent {
                protected void dispatch() {}
            }
            """;
        Files.writeString(tempDir.resolve("App.java"), source);
        CodebaseContext context = new CodebaseContext();
        context.scan(tempDir);

        TypeDeclaration child = context.getTypeDeclaration("com.example.Child");
        String target = InheritanceCallTargetResolver.resolveSuperMethod(context, child, "dispatch");
        assertThat(target).isEqualTo("com.example.Parent.dispatch");
    }
}
