package logic

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

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

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

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

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

func (l *GetUsersByIdsLogic) GetUsersByIds(in *pb.GetUsersByIdsReq) (*pb.GetUsersByIdsResp, error) {
	if len(in.Ids) == 0 {
		return &pb.GetUsersByIdsResp{}, nil
	}

	userList, err := l.svcCtx.UsersModelRO.Users.Query().Where(users.IDIn(in.Ids...)).All(l.ctx)
	if err != nil {
		return nil, err
	}

	return &pb.GetUsersByIdsResp{Users: ConvertEntUsersToProto(userList)}, nil
}
