package handler

import (
	"testing"
	"time"

	"github.com/denisovdennis/autohero/internal/model"
)

func TestConsumeFreeBuffCharge_SubscriberConsumesCharge(t *testing.T) {
	now := time.Now()
	pe := now.Add(time.Hour)
	h := &model.Hero{
		SubscriptionActive:       true,
		BuffFreeChargesRemaining: 10,
		BuffCharges: map[string]model.BuffChargeState{
			string(model.BuffRush): {Remaining: 2, PeriodEnd: &pe},
		},
	}
	if err := consumeFreeBuffCharge(h, model.BuffRush, now); err != nil {
		t.Fatal(err)
	}
	st := h.BuffCharges[string(model.BuffRush)]
	if st.Remaining != 1 {
		t.Fatalf("subscriber should consume charge, want remaining 1, got %d", st.Remaining)
	}
}

func TestConsumeFreeBuffCharge_Exhausted(t *testing.T) {
	end := time.Now().Add(time.Hour)
	h := &model.Hero{
		BuffFreeChargesRemaining: 0,
		BuffQuotaPeriodEnd:       &end,
		BuffCharges: map[string]model.BuffChargeState{
			string(model.BuffRush): {Remaining: 0, PeriodEnd: &end},
		},
	}
	if err := consumeFreeBuffCharge(h, model.BuffRush, time.Now()); err == nil {
		t.Fatal("expected error when exhausted")
	}
}
