package sms

import (
	"context"
	"errors"
	"fmt"
)

var (
	ErrProviderConfigInvalid = errors.New("sms provider config invalid")
	ErrProviderRateLimited   = errors.New("sms provider rate limited")
)

type Provider interface {
	SendLoginCode(ctx context.Context, phone string, code string) error
}

type ProviderError struct {
	Code    string
	Message string
	Err     error
}

func (e *ProviderError) Error() string {
	if e.Message != "" {
		return fmt.Sprintf("sms provider rejected: %s: %s", e.Code, e.Message)
	}
	if e.Code != "" {
		return fmt.Sprintf("sms provider rejected: %s", e.Code)
	}
	return "sms provider rejected"
}

func (e *ProviderError) Unwrap() error {
	return e.Err
}

func IsProviderRateLimited(err error) bool {
	return errors.Is(err, ErrProviderRateLimited)
}
