package match

import (
	"context"
	"time"

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

// 执行通过，加为好友
func DoJoin(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

	if in.Identity == "" {
		return nil, errcode.ErrInvalidArgument
	}

	data := &models.RelationFollow{
		FromIdentity: auth.Identity,
		ToIdentity:   in.Identity,
	}
	data.Identity = utils.UUID()

	err = impl.DBService.Create(data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return &pb.StatusReply{
		Data:    vars.OK,
		Timeseq: time.Now().UnixMilli(),
	}, nil

}
