package smtp

import "context"

// GuardedSender wraps Sender with a per-account SMTP circuit breaker.
type GuardedSender struct {
	inner   *Sender
	circuit *CircuitBreaker
}

func NewGuardedSender(inner *Sender, circuit *CircuitBreaker) *GuardedSender {
	return &GuardedSender{inner: inner, circuit: circuit}
}

func (g *GuardedSender) Send(ctx context.Context, req *SendRequest) error {
	if err := g.circuit.Allow(req.AccountID); err != nil {
		return err
	}
	err := g.inner.Send(ctx, req)
	if err != nil {
		g.circuit.RecordFailure(req.AccountID)
		return err
	}
	g.circuit.RecordSuccess(req.AccountID)
	return nil
}
