package logic

import (
	"context"
	"errors"
	"juwan-backend/app/snowflake/rpc/snowflake"

	"juwan-backend/app/users/rpc/internal/svc"
	"juwan-backend/app/users/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddUserFollowsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddUserFollowsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserFollowsLogic {
	return &AddUserFollowsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// -----------------------userFollows-----------------------
func (l *AddUserFollowsLogic) AddUserFollows(in *pb.AddUserFollowsReq) (*pb.AddUserFollowsResp, error) {
	idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
	if err != nil {
		logx.WithContext(l.ctx).Errorf("AddUserFollows error: %s", err.Error())
		return nil, err
	}

	_, err = l.svcCtx.UsersModelRW.UserFollows.Create().
		SetFollowerID(in.FollowerId).
		SetFolloweeID(in.FolloweeId).
		SetID(idResp.Id).
		Save(l.ctx)
	if err != nil {
		logx.WithContext(l.ctx).Errorf("AddUserFollows error: %s", err.Error())
		return nil, errors.New("failed to add user follow relationship")
	}
	return &pb.AddUserFollowsResp{}, nil
}
