package follow

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

// 关注状态
func State(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

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

	var data models.RelationFollow
	err = impl.DBService.Where("from_identity=? and to_identity=?", auth.Identity, in.Identity).First(&data).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return &pb.StatusReply{
				Data:    vars.OK,
				Timeseq: time.Now().UnixMilli(),
			}, nil
		} else {
			printer.Error(err.Error())
			return nil, errcode.ErrDB
		}
	}

	return &pb.StatusReply{
		Data:    data.Identity,
		Timeseq: time.Now().UnixMilli(),
	}, nil

}
