package captcha

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/svc"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"

	"net/http"
)

type GetCaptchaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	r      *http.Request
}

func NewGetCaptchaLogic(r *http.Request, ctx context.Context, svcCtx *svc.ServiceContext) *GetCaptchaLogic {
	return &GetCaptchaLogic{
		r:      r,
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetCaptchaLogic) GetCaptcha() (resp *types.CaptchaInfo, err error) {
	id, b64s, err := l.svcCtx.Captcha.Generate()
	if err != nil {
		return nil, err
	}
	resp = &types.CaptchaInfo{
		CaptchaId: id,
		Base64:    b64s,
	}
	return
}
