package aliyun_avatar

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 GetAliyunAvatarByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

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

func (l *GetAliyunAvatarByIdLogic) GetAliyunAvatarById(req *types.IDReq) (*types.AliyunAvatarInfoResp, error) {
	data, err := l.svcCtx.DB.AliyunAvatar.Get(l.ctx, req.Id)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.AliyunAvatarInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Code: 0,
			Msg:  errormsg.Success,
		},
		Data: types.AliyunAvatarInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &data.ID,
				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
			},
			UserId:          &data.UserID,
			BizId:           &data.BizID,
			AccessKeyId:     &data.AccessKeyID,
			AccessKeySecret: &data.AccessKeySecret,
			AppId:           &data.AppID,
			TenantId:        &data.TenantID,
			Response:        &data.Response,
			Token:           &data.Token,
			SessionId:       &data.SessionID,
		},
	}, nil
}
