package logic

import (
	"context"
	"errors"
	"juwan-backend/app/users/rpc/internal/models/userfollows"

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

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

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

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

func (l *DelUserFollowsLogic) DelUserFollows(in *pb.DelUserFollowsReq) (*pb.DelUserFollowsResp, error) {
	_, err := l.svcCtx.UsersModelRW.UserFollows.Delete().Where(
		userfollows.IDEQ(in.Id),
		userfollows.FollowerIDEQ(in.UserId),
	).Exec(l.ctx)

	if err != nil {
		logx.WithContext(l.ctx).Errorf("Failed to delete user follow: %s", err.Error())
		return nil, errors.New("failed to unfollow")
	}
	return &pb.DelUserFollowsResp{}, nil
}
