
package dev.sonpx.loyalty.mcp.reward.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Arrays;
import java.util.Locale;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 * Created by SonPhung on Monday, 20-Jul-2026
 */
@Getter
@RequiredArgsConstructor
public enum MerchantEntityLevel {
    CHAIN("C"),
    CORPORATION("CO"),
    STORE("S"),
    TERMINAL("T");

    @JsonValue
    private final String code;

    @JsonCreator
    public static MerchantEntityLevel fromCode(String code) {
        MerchantEntityLevel level = Arrays.stream(values())
                .filter(e -> e.code.equalsIgnoreCase(code) || e.name().equalsIgnoreCase(code))
                .findFirst()
                .orElse(null);
        if (level != null) {
            return level;
        }
        return switch (code.toUpperCase(Locale.ROOT)) {
            case "COR" -> CORPORATION;
            case "STO" -> STORE;
            case "CHA" -> CHAIN;
            case "TER" -> TERMINAL;
            default -> null;
        };
    }
}
