package handler

import (
	"kra/internal/service"
	"kra/internal/service/dto"

	"github.com/gin-gonic/gin"
)

type Email struct{ service *service.EmailService }

func NewEmail(service *service.EmailService) *Email { return &Email{service: service} }

func (h *Email) Test(c *gin.Context) {
	if err := h.service.Test(c.Request.Context()); err != nil {
		Fail(c, "发送失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "发送成功")
}

func (h *Email) Send(c *gin.Context) {
	var req dto.SendEmailRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		Fail(c, err.Error())
		return
	}
	if err := h.service.Send(c.Request.Context(), req.To, req.Subject, req.Body); err != nil {
		Fail(c, "发送失败")
		return
	}
	Write(c, CodeSuccess, gin.H{}, "发送成功")
}
