package api

import (
	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"wireguard-dashboard/component"
	"wireguard-dashboard/utils"
)

type captcha struct{}

func Captcha() captcha {
	return captcha{}
}

// GenerateCaptcha
// @description: 生成验证码
// @receiver captcha
// @param c
func (captcha) GenerateCaptcha(c *gin.Context) {
	math := base64Captcha.DriverMath{Height: 120, Width: 480, Fonts: []string{"ApothecaryFont.ttf", "3Dumb.ttf"}}
	mathDriver := math.ConvertFonts()

	capt := base64Captcha.NewCaptcha(mathDriver, component.CaptchaStore{})

	id, base64Str, _, err := capt.Generate()
	if err != nil {
		utils.GinResponse(c).FailedWithErr("生成验证码失败: %v", err)
		return
	}

	utils.GinResponse(c).OKWithData(map[string]any{
		"id":      id,
		"captcha": base64Str,
	})
}
