package com.qqchen.deploy.backend.workflow.enums;

import lombok.Getter;

@Getter
public enum WorkflowNodeInstanceStatusEnums {

    /**
     * 未开始：节点尚未开始执行
     */
    NOT_STARTED("NOT_STARTED", "未开始"),

    /**
     * 运行中：流程实例正在执行
     */
    RUNNING("RUNNING", "运行中"),

    /**
     * 已暂停：流程实例被手动暂停
     */
    SUSPENDED("SUSPENDED", "已暂停"),

    /**
     * 已完成：流程实例正常完成
     */
    COMPLETED("COMPLETED", "已完成"),

    /**
     * 已终止：流程实例被手动终止
     */
    TERMINATED("TERMINATED", "已终止"),

    /**
     * 执行失败：流程实例执行过程中发生错误
     */
    FAILED("FAILED", "执行失败");

    /**
     * 状态编码
     */
    private final String code;

    /**
     * 状态描述
     */
    private final String description;

    WorkflowNodeInstanceStatusEnums(String code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * 根据状态编码获取枚举实例
     *
     * @param code 状态编码
     * @return 对应的枚举实例
     */
    public static WorkflowNodeInstanceStatusEnums fromCode(String code) {
        for (WorkflowNodeInstanceStatusEnums status : WorkflowNodeInstanceStatusEnums.values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException("Invalid workflow instance status code: " + code);
    }

    /**
     * 判断是否为终态（不可再变化的状态）
     */
    public boolean isFinalState() {
        return this == COMPLETED || this == TERMINATED || this == FAILED;
    }

    /**
     * 判断是否可以暂停
     */
    public boolean canBeSuspended() {
        return this == RUNNING;
    }

    /**
     * 判断是否可以恢复
     */
    public boolean canBeResumed() {
        return this == SUSPENDED;
    }

    /**
     * 判断是否可以终止
     */
    public boolean canBeTerminated() {
        return this == RUNNING || this == SUSPENDED;
    }

    @Override
    public String toString() {
        return this.code;
    }
}
