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

import click.kamil.examples.statemachine.extended.constants.MyEvents;
import lombok.RequiredArgsConstructor;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class OrderService {

    private final StateMachine<String, String> stateMachine;
    private final StateMachinePersister<String, String, String> persister;
    private final ExternalNotificationService notificationService;

    public void processSubmit() {
        // Business logic here...
        notificationService.sendExternalNotification("Submit processed");
        stateMachine.sendEvent(MyEvents.SUBMIT);
    }

    public void processCancel() {
        // More business logic...
        stateMachine.sendEvent(MyEvents.CANCEL);
    }

    public void resumeOrder(String orderId) throws Exception {
        persister.restore(stateMachine, orderId);
    }
}
