package pricing

import (
	"testing"
	"time"
)

func TestCalculateSelectsExactEffectivePrice(t *testing.T) {
	now := time.Date(2026, 8, 11, 0, 0, 0, 0, time.UTC)
	service := &Service{}
	service.current.Store(&priceSnapshot{prices: []Price{
		{ID: "exact", ProviderCode: "openai", ModelPattern: "gpt-5", InputMicrounitsPerMillion: 2_000_000, OutputMicrounitsPerMillion: 8_000_000, Currency: "USD", EffectiveFrom: now.Add(-time.Hour), Enabled: true},
		{ID: "wild", ProviderCode: "openai", ModelPattern: "gpt-*", InputMicrounitsPerMillion: 1, Currency: "USD", EffectiveFrom: now.Add(-time.Hour), Enabled: true},
	}})
	cost := service.Calculate("OPENAI", "gpt-5", 1_000, 500, now)
	if cost.PriceID != "exact" || cost.Microunits != 6_000 {
		t.Fatalf("unexpected cost %#v", cost)
	}
}

func TestCalculateHonorsEffectiveWindow(t *testing.T) {
	now := time.Now().UTC()
	past := now.Add(-time.Hour)
	service := &Service{}
	service.current.Store(&priceSnapshot{prices: []Price{{ID: "expired", ProviderCode: "p1", ModelPattern: "m", InputMicrounitsPerMillion: 1_000_000, Currency: "USD", EffectiveFrom: now.Add(-2 * time.Hour), EffectiveTo: &past, Enabled: true}}})
	if cost := service.Calculate("p1", "m", 100, 0, now); cost.Microunits != 0 {
		t.Fatalf("expired price selected: %#v", cost)
	}
}
