// Package grpcserver owns the shared gRPC process wiring used by backend services.
package grpcserver

import (
	"time"

	"google.golang.org/grpc"
	"hyapp/pkg/grpcshutdown"
	"hyapp/pkg/logx"
)

const defaultGracefulStopTimeout = 15 * time.Second

// New creates the standard unary gRPC server used by owner services.
// Business services still register protobuf handlers themselves; this helper only centralizes runtime middleware.
func New(serviceName string, options ...grpc.ServerOption) *grpc.Server {
	base := []grpc.ServerOption{grpc.UnaryInterceptor(logx.UnaryServerInterceptor(serviceName))}
	base = append(base, options...)
	return grpc.NewServer(base...)
}

// GracefulStop drains in-flight RPCs with the same timeout behavior across services.
func GracefulStop(server *grpc.Server, timeout time.Duration) {
	if timeout <= 0 {
		timeout = defaultGracefulStopTimeout
	}
	grpcshutdown.GracefulStop(server, timeout)
}
