package com.diagbot.enums;

import com.diagbot.core.KeyedNamed;
import java.util.Arrays;

public enum RuleTypeEnum implements KeyedNamed {

    EMPTY_ITEM(1, "空项"),
    LOGIC_ERROR(2, "逻辑性错误"),
    CONSISTENCY_ERROR(3, "一致性错误"),
    UNKNOWN(-1, "未知类型");

    private final int key;
    private final String name;

    RuleTypeEnum(int key, String name) {
        this.key = key;
        this.name = name;
    }

    // 根据int类型的key获取枚举
    public static RuleTypeEnum fromKey(int key) {
        return Arrays.stream(values())
                .filter(e -> e.key == key)
                .findFirst()
                .orElse(UNKNOWN);
    }

    // 根据String类型的原始值转换（兼容性处理）
    public static RuleTypeEnum fromRawValue(int key) {
        try {
            return fromKey(key);
        } catch (NumberFormatException e) {
            return UNKNOWN;
        }
    }

    @Override
    public int getKey() {
        return key;
    }

    @Override
    public String getName() {
        return name;
    }
}