package click.kamil.web;

import click.kamil.domain.OrderEvent;
import click.kamil.service.StateMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private StateMachineService stateMachineService;

    @PostMapping("/process")
    public String processOrder() {
        stateMachineService.sendMessageWithOutbox(OrderEvent.PROCESS, event -> {
            System.out.println("Faulty Outbox Action executed post-commit for event: " + event);
        });
        return "Order process event sent within transaction";
    }

    @PostMapping("/complete")
    public String completeOrder() {
        stateMachineService.sendMessage(OrderEvent.COMPLETE);
        return "Order complete event sent";
    }

    @PostMapping("/cancel")
    public String cancelOrder() {
        stateMachineService.sendMessage(OrderEvent.CANCEL);
        return "Order cancel event sent";
    }

    // --- Functional Quirks for Static Analysis ---

    // A getter returning a specific ENUM constant
    public OrderEvent getCompleteEvent() {
        return OrderEvent.COMPLETE;
    }

    // A specific function that performs the transition
    public Void triggerTransitionFunction(click.kamil.domain.IEvent event) {
        stateMachineService.sendMessage(event);
        return null;
    }

    @PostMapping("/functional-complete")
    public String functionalCompleteOrder() {
        // Pass the getter and the transition applier as method references
        stateMachineService.executeFunctionalTransition(
                this::getCompleteEvent, 
                this::triggerTransitionFunction
        );
        return "Functional complete event triggered via method references";
    }
}
