package logic

import (
	"context"

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

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

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

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

func (l *NextIdLogic) NextId(_ *snowflake.NextIdReq) (*snowflake.NextIdResp, error) {
	id, err := l.svcCtx.Generator.NextID()
	if err != nil {
		l.Error("generator.NextID", "err", err)
		return nil, err
	}
	return &snowflake.NextIdResp{Id: id}, nil
}
