package employee

import (
	"context"
	"time"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/jwt"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetEmployeeSearchTestLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetEmployeeSearchTestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEmployeeSearchTestLogic {
	return &GetEmployeeSearchTestLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GetEmployeeSearchTestLogic) GetEmployeeSearchTest(req *types.AuthReq) (*types.AuthResp, error) {
	var token, secretKey string
	var err error
	var decryptData map[string]interface{}
	var ok bool
	var userId interface{}
	secretKey = l.svcCtx.Config.Auth.AccessSecret
	data := req.Data

	dataArray := make([]jwt.Option, 0)
	dataArray = append(dataArray, jwt.Option{
		Key: "userId",
		Val: data,
	})
	token, err = jwt.NewJwtToken(
		l.svcCtx.Config.Auth.AccessSecret,
		time.Now().Unix(),
		86400*14,
		dataArray...,
	)

	decryptData, err = jwt.ParseJwtToken(l.svcCtx.Config.Auth.AccessSecret, token)
	if err != nil {
		return nil, err
	}

	userId, ok = decryptData["userId"]
	return &types.AuthResp{
		Token:     &token,
		Data:      &data,
		Original:  userId,
		OK:        ok,
		SecretKey: secretKey,
	}, nil
}
