package friend

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 TagDoCreate(ctx context.Context, in *pb.TagDoCreateRequest) (reply *pb.StatusReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

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

	data := &models.FriendTag{Name: in.TagName}
	data.Identity = utils.UUID()
	data.PassportID = auth.ID
	data.PassportIdentity = auth.Identity

	err = impl.DBService.Create(data).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	batchs := make([]*models.FriendInTag, 0)
	for _, identity := range in.FriendIdentity {
		temp := &models.FriendInTag{
			FriendIdentity: identity,
			TagIdentity:    data.Identity,
		}
		temp.PassportID = auth.ID
		temp.PassportIdentity = auth.Identity

		batchs = append(batchs, temp)
	}

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

}
