package com.zt.plat.module.tmpltp.enums;

import java.util.HashSet;
import java.util.Set;

public enum UpdateStatusEnum {

    STATUS_DRF(TmplStsEnum.DRAFT.getCode(), new HashSet<>() {{
        add(TmplStsEnum.DRAFT.getCode());
        add(TmplStsEnum.STOPPED.getCode());
    }}),
    STATUS_PUB(TmplStsEnum.PUBLISHED.getCode(), new HashSet<>()),
    STATUS_STOP(TmplStsEnum.STOPPED.getCode(), new HashSet<>() {{
        add(TmplStsEnum.DRAFT.getCode());
        add(TmplStsEnum.STOPPED.getCode());
    }}),
    STATUS_START(TmplStsEnum.START.getCode(), new HashSet<>());


    private final String code;
    private final Set<String> allowedTransitions;

    UpdateStatusEnum(String code, Set<String> allowedTransitions) {
        this.code = code;
        this.allowedTransitions = allowedTransitions;
    }

    /**
     * 根据状态码获取对应的枚举实例
     */
    public static UpdateStatusEnum fromCode(String code) {
        for (UpdateStatusEnum status : values()) {
            if (status.code.equals(code)) {
                return status;
            }
        }
        return null;
    }

    /**
     * 校验状态转换是否合法
     */
    public boolean isTransitionAllowed(String targetStatus) {
        return allowedTransitions.contains(targetStatus);
    }
}
