package shop.alien.entity.store;

/**
 * 推送设备绑定归属类型，与 JWT 中 userType 及表 life_user_push_device.owner_type 一致。
 * <ul>
 *   <li>{@link #USER} — C 端 life_user.id</li>
 *   <li>{@link #STORE} — 门店 store_user.id</li>
 *   <li>{@link #LAWYER} — 律师 lawyer_user.id</li>
 * </ul>
 */
public enum PushDeviceOwnerType {

    USER("user"),
    STORE("store"),
    LAWYER("lawyer");

    private final String code;

    PushDeviceOwnerType(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    /**
     * 根据登录 token 中的 userType 解析；不支持的类型返回 null。
     */
    public static PushDeviceOwnerType fromJwtUserType(String jwtUserType) {
        if (jwtUserType == null || jwtUserType.trim().isEmpty()) {
            return USER;
        }
        String t = jwtUserType.trim().toLowerCase();
        switch (t) {
            case "user":
                return USER;
            case "store":
            case "merchant":
                return STORE;
            case "lawyer":
                return LAWYER;
            default:
                return null;
        }
    }

    public static PushDeviceOwnerType fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            return null;
        }
        String c = code.trim().toLowerCase();
        for (PushDeviceOwnerType v : values()) {
            if (v.code.equals(c)) {
                return v;
            }
        }
        return null;
    }
}
