package token

import (
	"context"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"

	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

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

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

func (l *GetTokenByIdLogic) GetTokenById(req *types.IDReq) (*types.TokenInfoResp, error) {
	data, err := l.svcCtx.DB.Token.Get(l.ctx, req.Id)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	expireAtStr := data.ExpireAt.Format("2006-01-02 15:04:05")
	return &types.TokenInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.TokenInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			ExpireAt:        pointy.GetUnixMilliPointer(data.ExpireAt.UnixMilli()),
			ExpireAtStr:     &expireAtStr,
			Token:           &data.Token,
			Mac:             &data.MAC,
			OrganizationId:  &data.OrganizationID,
			AgentId:         &data.AgentID,
			CustomAgentBase: &data.CustomAgentBase,
			CustomAgentKey:  &data.CustomAgentKey,
			OpenaiBase:      &data.OpenaiBase,
			OpenaiKey:       &data.OpenaiKey,
		},
	}, nil
}
