package friend

import (
	"context"

	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
	"bsm/full/module/social/relation/internal/impl"
	"bsm/full/module/social/relation/internal/logic/common"
	"bsm/full/module/social/relation/internal/models"
	pb "bsm/full/module/social/relation/pb"
)

// 好友申请详情
func ApplyGet(ctx context.Context, in *pb.IdentRequest) (reply *pb.FriendApplyGetReply, err error) {
	_, err = service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	if in.Identity == "" {
		return nil, errcode.ErrInvalidArgument
	}

	var apply models.FriendApply
	err = impl.DBService.Where("identity=?", in.Identity).First(&apply).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	reply.Data, err = common.GetrelationInfoDetailCard(apply.FromIdentity)
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	err = impl.DBService.Where("apply_id=?", apply.ID).Order("id asc").Find(&reply.Message).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return
}
