package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"log/slog"
	"slgserver/constant"
	myhttp "slgserver/server/httpserver"
	"slgserver/server/httpserver/logic"
	"slgserver/server/httpserver/middleware"
)

type AccountController struct{}

func (self AccountController) RegisterRoutes(g *gin.RouterGroup) {
	g.Use(middleware.Cors())
	
	g.Any("/account/register", self.register)
	g.Any("/account/changepwd", self.changePwd)
	g.Any("/account/forgetpwd", self.forgetPwd)
	g.Any("/account/resetpwd", self.resetPwd)
}

func (self AccountController) register(ctx *gin.Context) {
	slog.Info("register")
	data := make(map[string]interface{})
	if err := logic.DefaultUser.CreateUser(ctx); err != nil {
		data["code"] = err.(*myhttp.MyError).Id()
		data["errmsg"] = err.(*myhttp.MyError).Error()
	}else{
		data["code"] = constant.OK
	}

	ctx.JSON(http.StatusOK, data)
}

func (self AccountController) forgetPwd(ctx *gin.Context) {
	slog.Info("forgetPwd")
	ctx.String(http.StatusOK, "forgetPwd")
}

func (self AccountController) changePwd(ctx *gin.Context) {
	slog.Info("changePwd")
	data := make(map[string]interface{})
	if err := logic.DefaultUser.ChangePassword(ctx); err != nil {
		data["code"] = err.(*myhttp.MyError).Id()
		data["errmsg"] = err.(*myhttp.MyError).Error()
	}else{
		data["code"] = constant.OK
	}

	ctx.JSON(http.StatusOK, data)
}

func (self AccountController) resetPwd(ctx *gin.Context) {
	slog.Info("resetPwd")
	ctx.String(http.StatusOK, "resetPwd")
}

