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

import click.kamil.examples.statemachine.layered.command.DomainCommand;
import click.kamil.examples.statemachine.layered.dispatch.CentralEventDispatcher;
import click.kamil.examples.statemachine.layered.dispatch.StringCommandMapper;
import click.kamil.examples.statemachine.layered.order.PayRichOrderEvent;
import click.kamil.examples.statemachine.layered.order.ShipRichOrderEvent;
import org.springframework.stereotype.Service;

@Service
public class CommandGateway {

    private final StringCommandMapper stringCommandMapper;
    private final CentralEventDispatcher centralEventDispatcher;

    public CommandGateway(StringCommandMapper stringCommandMapper, CentralEventDispatcher centralEventDispatcher) {
        this.stringCommandMapper = stringCommandMapper;
        this.centralEventDispatcher = centralEventDispatcher;
    }

    public void payOrder() {
        centralEventDispatcher.orderPay();
    }

    public void shipOrder() {
        centralEventDispatcher.orderShip();
    }

    public void cancelOrder() {
        centralEventDispatcher.orderCancel();
    }

    public void payOrderViaRichEvent(PayRichOrderEvent event) {
        centralEventDispatcher.orderPayViaRichEvent(event);
    }

    public void shipOrderViaRichEvent(ShipRichOrderEvent event) {
        centralEventDispatcher.orderShipViaRichEvent(event);
    }

    public void submitDocument() {
        centralEventDispatcher.documentSubmit();
    }

    public void approveDocument() {
        centralEventDispatcher.documentApprove();
    }

    public void rejectDocument() {
        centralEventDispatcher.documentReject();
    }

    /**
     * Full two-hop dispatch used by {@link GenericCommandController}:
     * string key → {@link DomainCommand} → transition event.
     */
    public void executeViaMapper(String commandKey) {
        DomainCommand command = stringCommandMapper.fromString(commandKey);
        centralEventDispatcher.dispatch(command);
    }
}
