package camel_oil

import (
	"context"

	"github.com/gogf/gf/v2/errors/gcode"
	v1 "kami/api/camel_oil/v1"
	"kami/internal/consts"
	"kami/internal/errHandler"
	"kami/internal/model"
	"kami/internal/service"
)

func (c *ControllerV1) GetToken(ctx context.Context, req *v1.GetTokenReq) (res *v1.GetTokenRes, err error) {
	_, err = service.SysAuth().LoginOnlyLogin(ctx)
	if err != nil {
		err = errHandler.WrapError(ctx, gcode.CodeInternalError, err, "登录校验失败")
		return
	}
	token, err := service.CamelOil().GetTokenInfo(ctx, &model.CamelOilTokenQueryInput{
		TokenId: req.TokenId,
	})
	if err != nil {
		return nil, err
	}
	return &v1.GetTokenRes{
		Token: v1.TokenInfo{
			Id:                  token.Id,
			UserId:              token.UserId,
			Name:                token.Name,
			LoginToken:          token.LoginToken,
			Phone:               token.Phone,
			Status:              consts.CamelOilTokenStatus(token.Status),
			BindCount:           token.BindCount,
			TotalBindAmount:     token.TotalBindAmount.InexactFloat64(),
			TotalRechargeAmount: token.TotalRechargeAmount.InexactFloat64(),
			RechargeLimitAmount: token.RechargeLimitAmount.InexactFloat64(),
			RechargeLimitCount:  token.RechargeLimitCount,
			LastBindAt:          token.LastBindAt,
			LastUsedAt:          token.LastUsedAt,
			Remark:              token.Remark,
			CreatedAt:           token.CreatedAt,
			UpdatedAt:           token.UpdatedAt,
		},
	}, nil
}
