package admin

import (
	"blazing/common/data/share"
	"blazing/cool"
	baseservice "blazing/modules/base/service"
	"blazing/modules/blazing/service"

	"context"
	"fmt"

	"github.com/gogf/gf/v2/frame/g"
)

type SessionReq struct {
	g.Meta   `path:"/getSessionByAuth" method:"GET"`
	Email    string `json:"email" v:"required|email"`
	Password string `json:"password"  v:"required"`
}
type SessionRes struct {
	Msg     string `json:"msg"`
	Code    int    `json:"code"`
	Session string `json:"session"`
}

type BlazingController struct {
	*cool.Controller
}

var biazing_service = service.NewLoginServiceService()

func init() {
	var blazing_controller = &BlazingController{
		&cool.Controller{
			Prefix:  "/seer/game",
			Api:     []string{},
			Service: biazing_service,
		},
	}
	// 注册路由
	cool.RegisterController(blazing_controller)
}
func (c *BlazingController) GetSession(ctx context.Context, req *SessionReq) (res *SessionRes, err error) {
	// res = &DemoSampleWelcomeRes{
	// 	BaseRes: cool.Ok("Welcome to Cool Admin Go"),
	// 	Data:    gjson.New(`{"name": "Cool Admin Go", "age":0}`),
	// }
	res = &SessionRes{
		Msg:     "success",
		Code:    200,
		Session: ""}
	if err := g.Validator().Data(req).Run(ctx); err != nil {
		fmt.Println(err)
		res.Code = 400
		res.Msg = err.Error()

	}
	res1, err := baseservice.NewBaseSysUserService().GetSession(req.Email, req.Password)

	if err != nil || res1 == nil {
		res.Code = 400
		res.Msg = err.Error()
		return
	}

	accountID := res1.ID
	retsid, sid, err := biazing_service.GetSessionId(accountID)
	if err != nil {
		res.Code = 400
		res.Msg = err.Error()

	}

	res.Session = retsid

	if err := share.ShareManager.SaveSession(sid, uint32(accountID)); err != nil {
		res.Code = 400
		res.Msg = err.Error()
	}

	return
}
