package click.kamil.enterprise.machines.order;

import click.kamil.enterprise.core.AbstractStateMachine;
import click.kamil.enterprise.core.TransitionContext;
import org.springframework.stereotype.Component;

@Component
public class OrderStateMachine extends AbstractStateMachine<OrderState, OrderEvent> {

    public OrderStateMachine() {
        super(OrderState.NEW);
    }

    @Override
    public String getMachineType() {
        return "ORDER";
    }

    @Override
    protected void configureTransitions() {
        addTransition(OrderEvent.PAY, OrderState.PENDING);
        addTransition(OrderEvent.SHIP, OrderState.SHIPPED);
    }

    @Override
    protected void executePreChecks(OrderState from, OrderState to, TransitionContext<?> context) {
        super.executePreChecks(from, to, context);
        if (to == OrderState.SHIPPED && from != OrderState.PENDING) {
            throw new IllegalStateException("Must be PENDING to ship!");
        }
        System.out.println("[Order] Specific pre-checks passed for order.");
    }

    @Override
    protected void onTransition(OrderState from, OrderState to, OrderEvent event, TransitionContext<?> context) {
        System.out.println("[Order] Transitioned from " + from + " to " + to + " via " + event);
    }
}
