package click.kamil.web;

import click.kamil.domain.OrderEvent;
import click.kamil.service.StateMachineService;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class JmsOrderListener {

    private final StateMachineService stateMachineService;

    // Constructor injection
    public JmsOrderListener(StateMachineService stateMachineService) {
        this.stateMachineService = stateMachineService;
    }

    @JmsListener(destination = "order.process.queue")
    public void receiveProcessCommand(String message) {
        System.out.println("Received JMS message: " + message);
        stateMachineService.sendMessageWithOutbox(OrderEvent.PROCESS, event -> {
            System.out.println("JMS Triggered Faulty Outbox Action executed post-commit for event: " + event);
        });
    }

    @JmsListener(destination = "order.cancel.queue")
    public void receiveCancelCommand(String message) {
        System.out.println("Received JMS message: " + message);
        stateMachineService.sendMessageWithProvider(() -> OrderEvent.CANCEL);
    }
}
