package account

import (
	"context"

	"bsm/full/module/base/passport/internal/impl"
	"bsm/full/module/base/passport/internal/models"
	pb "bsm/full/module/base/passport/pb"
	"git.apinb.com/bsm-sdk/core/service"
)

// 通过会员所有信息
func Get(ctx context.Context, in *pb.Empty) (reply *pb.GetFullReply, err error) {
	AUTH, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

	account, err := models.GetPassportAccountByField("id", AUTH.ID)
	if err != nil {
		return nil, err
	}

	tagRecords := make([]models.PassportTags, 0)
	impl.DBService.Model(&models.PassportTags{}).Where("passport_id=?", AUTH.ID).Find(&tagRecords)

	tags := make([]*pb.TagItem, 0)
	for _, tag := range tagRecords {
		tags = append(tags, &pb.TagItem{
			Name: tag.Name,
			Icon: tag.Icon,
		})
	}

	reply = &pb.GetFullReply{
		Identity: account.Identity,
		Account:  account.Account,
		Phone:    account.Phone,
		Email:    account.Email,
		Tags:     tags,
	}

	data := &models.PassportData{}
	impl.DBService.Where("passport_id=?", AUTH.ID).First(data)
	if data != nil {
		bd := data.Birthday.Format("2006-01-02")
		if bd == "0001-01-01" {
			bd = ""
		}
		reply.Rights = data.Rights
		reply.Nickname = data.Nickname
		reply.Avatar = data.Avatar
		reply.Birthday = bd
		reply.Sex = int32(data.Sex)
		reply.Country = data.Country
		reply.Province = data.Province
		reply.City = data.City
		reply.Area = data.Area
		reply.Sign = data.Sign
		reply.Cover = data.Cover
		reply.Score = data.Score
		reply.Level = data.Level
		reply.VerifyStatus = &pb.VerifyStatus{
			EmailVerify:    data.EmailVerify,
			PhoneVerify:    data.PhoneVerify,
			FaceVerify:     data.FaceVerify,
			DocumentVerify: data.DocumentVerify,
			KycVerify:      data.KycVerify,
		}
	}

	return
}
