package jnpf.model.franchisee.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Arrays;

/**
 * 加盟商自定义字段类型
 */
public enum FranchiseeCustomFieldTypeEnum {

    /**
     * 文本
     */
    TEXT("文本"),

    /**
     * 多选项
     */
    MULTI_OPTION("多选项"),

    /**
     * 图片
     */
    IMAGE("图片");

    private final String type;

    FranchiseeCustomFieldTypeEnum(String type) {
        this.type = type;
    }

    @JsonValue
    public String getType() {
        return type;
    }

    /**
     * 根据字符串解析字段类型，兼容枚举名、中文值和常见英文值
     *
     * @param type 字段类型文本
     * @return 字段类型枚举
     */
    @JsonCreator
    public static FranchiseeCustomFieldTypeEnum getByType(String type) {
        if (type == null) {
            return null;
        }
        String normalized = type.trim();
        if (normalized.isEmpty()) {
            return null;
        }
        if ("text".equalsIgnoreCase(normalized)) {
            return TEXT;
        }
        if ("multiOption".equalsIgnoreCase(normalized) || "multi-option".equalsIgnoreCase(normalized)) {
            return MULTI_OPTION;
        }
        if ("image".equalsIgnoreCase(normalized)) {
            return IMAGE;
        }
        return Arrays.stream(values())
                .filter(item -> item.type.equalsIgnoreCase(normalized) || item.name().equalsIgnoreCase(normalized))
                .findFirst()
                .orElse(null);
    }
}

