package logic

import (
	"context"
	"errors"

	"juwan-backend/app/snowflake/rpc/internal/svc"
	"juwan-backend/app/snowflake/rpc/snowflake"

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

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

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

func (l *NextIdsLogic) NextIds(in *snowflake.NextIdsReq) (*snowflake.NextIdsResp, error) {
	if in.Count <= 0 || in.Count > 1000 {
		return nil, errors.New("count must be between 1 and 1000")
	}
	ids, err := l.svcCtx.Generator.NextIDs(int(in.Count))
	if err != nil {
		l.Errorf("generate snowflake ids failed: %v", err)
		return nil, err
	}
	return &snowflake.NextIdsResp{Ids: ids}, nil
}
