package system

import (
	"context"
	"errors"
	"strings"
)

type EmailRepo interface {
	Send(context.Context, []string, string, string) error
	DefaultRecipients() []string
}

type EmailUsecase struct{ repo EmailRepo }

func NewEmailUsecase(repo EmailRepo) *EmailUsecase { return &EmailUsecase{repo: repo} }

func (uc *EmailUsecase) Send(ctx context.Context, to, subject, body string) error {
	return uc.repo.Send(ctx, strings.Split(to, ","), subject, body)
}

func (uc *EmailUsecase) Test(ctx context.Context) error {
	recipients := uc.repo.DefaultRecipients()
	if len(recipients) == 0 {
		return errors.New("未配置测试邮件收件人")
	}
	return uc.repo.Send(ctx, recipients, "test", "test")
}
