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

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

/**
 * 状态枚举类，定义所有可能的状态及合法的状态转换
 */
public enum PublishStatusEnum {

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


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

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

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

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