;; lib/commerce/tests/window.sx — time-windowed promotions.
;; Uses (commerce-test name got expected) provided by conformance.sh.

(define
  pcat
  (make-catalog (list (list "widget" 1000 :standard)) (list) (list)))

(define gctx (make-pricing-context pcat (list) :uk :guest))
(define cart (list (list "widget" :none 3)))

(define ten (list :percent "TEN" :standard 1000))
(define twenty (list :percent "TWENTY" :standard 2000))
(define always (list :fixed "ALWAYS" 0 100))

(define
  windowed
  (list
    (windowed-promo ten 100 200)
    (windowed-promo twenty 150 300)
    (windowed-promo always nil nil)))

(define exclusions (list (list "TEN" "TWENTY")))

;; --- wp-active? boundaries (inclusive) ---

(commerce-test
  "active-at-from"
  (wp-active? (windowed-promo ten 100 200) 100)
  true)
(commerce-test
  "active-at-until"
  (wp-active? (windowed-promo ten 100 200) 200)
  true)
(commerce-test
  "inactive-before"
  (wp-active? (windowed-promo ten 100 200) 99)
  false)
(commerce-test
  "inactive-after"
  (wp-active? (windowed-promo ten 100 200) 201)
  false)
(commerce-test
  "open-ended-always"
  (wp-active? (windowed-promo always nil nil) 99999)
  true)
(commerce-test
  "open-lower"
  (wp-active? (windowed-promo ten nil 200) 1)
  true)
(commerce-test
  "open-upper"
  (wp-active? (windowed-promo ten 100 nil) 99999)
  true)

;; --- active-ruleset filtering ---

(commerce-test
  "active-ruleset-120"
  (active-ruleset windowed 120)
  (list ten always))
(commerce-test
  "active-ruleset-160"
  (active-ruleset windowed 160)
  (list ten twenty always))
(commerce-test
  "active-ruleset-250"
  (active-ruleset windowed 250)
  (list twenty always))
(commerce-test
  "active-ruleset-50"
  (active-ruleset windowed 50)
  (list always))

;; --- active-codes (backward query) ---

(commerce-test
  "active-codes-120"
  (active-codes windowed 120)
  (list "TEN" "ALWAYS"))
(commerce-test
  "active-codes-160"
  (active-codes windowed 160)
  (list "TEN" "TWENTY" "ALWAYS"))
(commerce-test
  "active-codes-50"
  (active-codes windowed 50)
  (list "ALWAYS"))

;; --- windowed-quote: discount changes with time (deterministic) ---
;; subtotal 3000, no tax. TEN=300, TWENTY=600, ALWAYS=100; TEN/TWENTY exclusive.

(commerce-test
  "quote-50"
  (quote-discount (windowed-quote gctx cart windowed exclusions 50))
  100)
(commerce-test
  "quote-120"
  (quote-discount (windowed-quote gctx cart windowed exclusions 120))
  400)
(commerce-test
  "quote-160"
  (quote-discount (windowed-quote gctx cart windowed exclusions 160))
  700)
(commerce-test
  "quote-250"
  (quote-discount (windowed-quote gctx cart windowed exclusions 250))
  700)

(commerce-test
  "quote-total-160"
  (quote-total (windowed-quote gctx cart windowed exclusions 160))
  2300)
