package friend

import (
	"context"
	"strings"
	"time"

	"bsm/full/module/social/relation/internal/impl"
	"bsm/full/module/social/relation/internal/models"
	pb "bsm/full/module/social/relation/pb"
	"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/vars"
)

// 更新标签成员
func TagDoUpdate(ctx context.Context, in *pb.TagDoUpdateRequest) (reply *pb.DataStatusReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	if in.TagIdentity == "" || in.FriendIdentity == "" {
		return nil, errcode.ErrInvalidArgument
	}

	switch strings.ToUpper(in.Direction) {
	case "ADD":
		data := models.FriendInTag{
			FriendIdentity: in.FriendIdentity,
			TagIdentity:    in.TagIdentity,
		}
		data.PassportID = auth.ID
		data.PassportIdentity = auth.Identity

		err = impl.DBService.Create(&data).Error
	case "DEL":
		err = impl.DBService.Model(models.FriendInTag{}).Delete("friend_identity=? and tag_identity=?", in.FriendIdentity, in.TagIdentity).Error
	default:
		return nil, errcode.ErrInvalidArgument
	}
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return &pb.DataStatusReply{
		Data:    vars.OK,
		Timeseq: time.Now().UnixMilli(),
	}, nil

}
