package limiter

import (
	"github.com/rose_cat707/luminary/internal/model"
	"github.com/rose_cat707/luminary/internal/store/cache"
)

func IsProviderKeyAvailable(k model.ProviderKey) bool {
	if !k.Enabled {
		return false
	}
	if k.MaxRequestsPerDay > 0 && k.RequestsToday >= k.MaxRequestsPerDay {
		return false
	}
	if k.MaxTokensPerDay > 0 && k.TokensToday >= k.MaxTokensPerDay {
		return false
	}
	return true
}

func CheckIngressBudget(k *model.IngressKey, estimatedCost float64) bool {
	if k.BudgetLimit <= 0 {
		return true
	}
	return k.BudgetUsed+estimatedCost <= k.BudgetLimit
}

func CheckRateLimit(c *cache.Store, ingressKeyID uint, rpm, tpm, tokens int) bool {
	return c.CheckRateLimit(ingressKeyID, rpm, tpm, tokens)
}
