package shop;

import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Requirement given to the agent, word for word: "Add a 10% discount for orders over Rs 1,000."
 *
 * THIS IS THE CORRECTED VERSION: strictly over 1000, goods subtotal only, shipping never discounted.
 */
@Service
public class PricingService {

    public record Quote(BigDecimal subtotal, BigDecimal shipping, BigDecimal discount, BigDecimal total) {}

    private static final BigDecimal THRESHOLD = new BigDecimal("1000");
    private static final BigDecimal RATE = new BigDecimal("0.10");

    public Quote quote(BigDecimal subtotal, BigDecimal shipping) {
        BigDecimal discount = subtotal.compareTo(THRESHOLD) > 0
                ? subtotal.multiply(RATE).setScale(2, RoundingMode.HALF_UP)
                : BigDecimal.ZERO.setScale(2);
        BigDecimal total = subtotal.add(shipping).subtract(discount).setScale(2, RoundingMode.HALF_UP);
        return new Quote(subtotal, shipping, discount, total);
    }
}
