package handler

import (
	"kra/internal/modules/system/dto"
	"kra/internal/modules/system/service"
	"kra/internal/modules/system/transport/httpx"

	"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 {
		httpx.Fail(c, "发送失败")
		return
	}
	httpx.Write(c, httpx.CodeSuccess, gin.H{}, "发送成功")
}

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