package logic

import (
	"context"
	"errors"

	"juwan-backend/app/user_verifications/rpc/internal/svc"
	"juwan-backend/app/user_verifications/rpc/pb"

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

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

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

func (l *GetUserVerificationsByIdLogic) GetUserVerificationsById(in *pb.GetUserVerificationsByIdReq) (*pb.GetUserVerificationsByIdResp, error) {
	userVerification, err := l.svcCtx.UserVeriModelRO.Get(l.ctx, in.Id)
	if err != nil {
		logx.Errorf("GetUserVerificationsById err: %v", err)
		return nil, errors.New("get VerificationsById err")
	}
	pbVerification := pb.UserVerifications{}
	err = copier.Copy(&pbVerification, userVerification)
	if err != nil {
		logx.Errorf("copier copy err: %v", err)
		return nil, errors.New("copy Verification err")
	}
	createAt := userVerification.CreatedAt.Unix()
	updateAt := userVerification.UpdatedAt.Unix()
	pbVerification.CreatedAt = createAt
	pbVerification.UpdatedAt = updateAt

	return &pb.GetUserVerificationsByIdResp{
		UserVerifications: &pbVerification,
	}, nil
}
