package order

import "bindbox-game/internal/repository/mysql/model"

func ApplyCouponDiscount(amount int64, c *model.SystemCoupons, minSpendOK bool) int64 {
    if c == nil || amount <= 0 || !minSpendOK { return 0 }
    switch c.DiscountType {
    case 1:
        return clamp(c.DiscountValue, 0, amount)
    case 2:
        return clamp(c.DiscountValue, 0, amount)
    case 3:
        rate := c.DiscountValue
        if rate < 0 { rate = 0 }
        if rate > 1000 { rate = 1000 }
        newAmt := amount * rate / 1000
        return clamp(amount - newAmt, 0, amount)
    default:
        return 0
    }
}

func clamp(v int64, min int64, max int64) int64 {
    if v < min { return min }
    if v > max { return max }
    return v
}
