package click.kamil.enterprise.machines.user;

import click.kamil.enterprise.core.AbstractStateMachineConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

@Configuration
@EnableStateMachineFactory(name = "userStateMachineFactory")
public class UserStateMachineConfiguration extends AbstractStateMachineConfiguration<UserState, UserEvent> {

    @Override
    protected void configureStates(StateMachineStateConfigurer<UserState, UserEvent> states) throws Exception {
        states
            .withStates()
            .initial(UserState.GUEST)
            .state(UserState.REGISTERED)
            .end(UserState.VERIFIED);
    }

    @Override
    protected void configureTransitions(StateMachineTransitionConfigurer<UserState, UserEvent> transitions) throws Exception {
        commonProtectedMethod();
        transitions
            .withExternal()
                .source(UserState.GUEST).target(UserState.REGISTERED).event(UserEvent.REGISTER)
                .and()
            .withExternal()
                .source(UserState.REGISTERED).target(UserState.VERIFIED).event(UserEvent.VERIFY);
    }
}
