package click.kamil.springstatemachineexporter.analysis.service;

import click.kamil.springstatemachineexporter.analysis.model.CallChain;
import click.kamil.springstatemachineexporter.model.Transition;
import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;

import static click.kamil.springstatemachineexporter.analysis.service.CentralDispatcherTestSupport.*;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * Interface accessor widen must not union all implementation return values at ambiguous call sites.
 */
class InterfaceAccessorPolyPipelineTest {

    private static final String MACHINE_CONFIG = "com.example.OrderStateMachineConfig";
    private static final String EVENT_TYPE = "com.example.OrderEvent";
    private static final String STATE_TYPE = "com.example.OrderState";

    @Test
    void linkerShouldNotMatchAllTransitionsForInterfaceTypedEventSource(@TempDir Path tempDir) throws Exception {
        String source = """
            package com.example;
            public class ApiController {
                EventHandler handler;
                public void dispatch() { handler.fire(); }
            }
            class EventHandler {
                StateMachine machine;
                EventSource source;
                void fire() { machine.sendEvent(source.getEvent()); }
            }
            interface EventSource { OrderEvent getEvent(); }
            class PaySource implements EventSource {
                public OrderEvent getEvent() { return OrderEvent.PAY; }
            }
            class ShipSource implements EventSource {
                public OrderEvent getEvent() { return OrderEvent.SHIP; }
            }
            class StateMachine { public void sendEvent(OrderEvent e) {} }
            enum OrderEvent { PAY, SHIP, LOG, META }
            class OrderStateMachineConfig {}
            """;
        CodebaseContext context = scanSource(source, tempDir);

        Transition pay = transition("NEW", "PAID", EVENT_TYPE + ".PAY");
        Transition ship = transition("PAID", "SHIPPED", EVENT_TYPE + ".SHIP");

        CallChain raw = resolveChain(context, "com.example.ApiController", "dispatch",
                "com.example.StateMachine", "sendEvent", EngineKind.JDT);

        CallChain linked = linkChain(
                context,
                raw,
                MACHINE_CONFIG,
                EVENT_TYPE,
                STATE_TYPE,
                pay,
                ship);

        assertThat(linked.getTriggerPoint().isAmbiguous()).isTrue();
        assertThat(linked.getMatchedTransitions()).isNullOrEmpty();
    }

    @Test
    void linkerShouldResolveConcretePaySourceImplementation(@TempDir Path tempDir) throws Exception {
        String source = """
            package com.example;
            public class ApiController {
                EventHandler handler;
                public void pay() { handler.fire(new PaySource()); }
            }
            class EventHandler {
                StateMachine machine;
                void fire(EventSource source) { machine.sendEvent(source.getEvent()); }
            }
            interface EventSource { OrderEvent getEvent(); }
            class PaySource implements EventSource {
                public OrderEvent getEvent() { return OrderEvent.PAY; }
            }
            class ShipSource implements EventSource {
                public OrderEvent getEvent() { return OrderEvent.SHIP; }
            }
            class StateMachine { public void sendEvent(OrderEvent e) {} }
            enum OrderEvent { PAY, SHIP, LOG, META }
            class OrderStateMachineConfig {}
            """;
        CodebaseContext context = scanSource(source, tempDir);

        Transition pay = transition("NEW", "PAID", EVENT_TYPE + ".PAY");
        Transition ship = transition("PAID", "SHIPPED", EVENT_TYPE + ".SHIP");

        CallChain linked = resolveLinkAndAssertSingleMatch(
                context,
                "com.example.ApiController",
                "pay",
                "com.example.StateMachine",
                "sendEvent",
                MACHINE_CONFIG,
                EVENT_TYPE,
                STATE_TYPE,
                EVENT_TYPE + ".PAY",
                EngineKind.JDT,
                pay,
                ship);

        assertPolyCappedToTransitionEvents(linked, EVENT_TYPE, pay, ship);
    }
}
