package com.xiang.xservice.application.script.fwd.constants;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Arrays;

@Getter
@AllArgsConstructor
public enum PriceRange {
    RANGE_600_900(0, BigDecimal.valueOf(600), BigDecimal.valueOf(900)),
    RANGE_900_1300(1, BigDecimal.valueOf(900), BigDecimal.valueOf(1300)),
    RANGE_1300_1600(2, BigDecimal.valueOf(1300), BigDecimal.valueOf(1600)),
    RANGE_OVER_1600(3, BigDecimal.valueOf(1600), null),
    RANGE_UNDER_600(4, null, BigDecimal.valueOf(600));

    private final int index;
    private final BigDecimal min;
    private final BigDecimal max;

    public boolean contains(BigDecimal price) {
        boolean minCondition = min == null || price.compareTo(min) > 0;
        boolean maxCondition = max == null || price.compareTo(max) <= 0;
        return minCondition && maxCondition;
    }


    public static PriceRange fromPrice(BigDecimal price) {
        return Arrays.stream(values())
                .filter(range -> range.contains(price))
                .findFirst()
                .orElseThrow(() -> new IllegalArgumentException("Invalid price: " + price));
    }
}
