package com.starry.admin.modules.shop.module.support;

/**
 * Encapsulates how coupon inventory should behave (finite vs. unlimited).
 */
public enum CouponInventoryPolicy {
    UNLIMITED,
    FINITE;

    public boolean isUnlimited() {
        return this == UNLIMITED;
    }

    public boolean consumesStock() {
        return this == FINITE;
    }

    public boolean hasStock(Integer remainingQuantity) {
        if (isUnlimited()) {
            return true;
        }
        return remainingQuantity == null || remainingQuantity > 0;
    }

    public static CouponInventoryPolicy resolve(Integer totalQuantity, Integer remainingQuantity) {
        if (isUnlimitedSentinel(totalQuantity) || isUnlimitedSentinel(remainingQuantity)) {
            return UNLIMITED;
        }
        if (totalQuantity == null && remainingQuantity == null) {
            return UNLIMITED;
        }
        return FINITE;
    }

    private static boolean isUnlimitedSentinel(Integer value) {
        return value != null && value < 0;
    }
}
