package click.kamil.enterprise.machines.document;

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 = "documentStateMachineFactory")
public class DocumentStateMachineConfiguration extends AbstractStateMachineConfiguration<DocumentState, DocumentEvent> {

    @Override
    protected void configureStates(StateMachineStateConfigurer<DocumentState, DocumentEvent> states) throws Exception {
        states
            .withStates()
            .initial(DocumentState.DRAFT)
            .state(DocumentState.IN_REVIEW)
            .end(DocumentState.APPROVED);
    }

    @Override
    protected void configureTransitions(StateMachineTransitionConfigurer<DocumentState, DocumentEvent> transitions) throws Exception {
        commonProtectedMethod();
        transitions
            .withExternal()
                .source(DocumentState.DRAFT).target(DocumentState.IN_REVIEW).event(DocumentEvent.SUBMIT)
                .and()
            .withExternal()
                .source(DocumentState.IN_REVIEW).target(DocumentState.APPROVED).event(DocumentEvent.APPROVE)
                .and()
            .withExternal()
                .source(DocumentState.IN_REVIEW).target(DocumentState.DRAFT).event(DocumentEvent.REJECT);
    }
}
