package click.kamil.examples.statemachine.extended.web;

import click.kamil.examples.statemachine.extended.constants.MyEvents;
import click.kamil.examples.statemachine.extended.service.OrderService;
import click.kamil.examples.statemachine.extended.service.ReactiveOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.statemachine.StateMachine;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

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

    private final StateMachine<String, String> stateMachine;
    private final OrderService orderService;
    private final ReactiveOrderService reactiveOrderService;

    @PostMapping("/submit")
    public void submitOrder() {
        orderService.processSubmit();
    }

    @PostMapping("/cancel")
    public void cancelOrder() {
        orderService.processCancel();
    }

    @PostMapping("/finish")
    public void finishOrder() {
        stateMachine.sendEvent("FINISH");
    }

    @PostMapping("/resume")
    public void resumeOrder(String orderId) throws Exception {
        orderService.resumeOrder(orderId);
    }

    @PostMapping("/reactive")
    public Mono<Void> reactiveOrder(String orderId) {
        return reactiveOrderService.processReactive(orderId);
    }
}
