package auth

import (
	"github.com/gin-gonic/gin"
	"github.com/perfect-panel/server/internal/logic/auth"
	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/internal/types"
	"github.com/perfect-panel/server/pkg/result"
)

func EmailLoginHandler(svcCtx *svc.ServiceContext) gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.EmailLoginRequest
		if err := c.ShouldBind(&req); err != nil {
			result.ParamErrorResult(c, err)
			return
		}

		req.IP = c.ClientIP()
		req.UserAgent = c.Request.UserAgent()

		if err := svcCtx.Validate(&req); err != nil {
			result.ParamErrorResult(c, err)
			return
		}

		l := auth.NewEmailLoginLogic(c.Request.Context(), svcCtx)
		resp, err := l.EmailLogin(&req)
		result.HttpResult(c, resp, err)
	}
}
