package agentk

import (
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v18/internal/module/kas2agentk_tunnel/router"
	"gitlab.com/gitlab-org/cluster-integration/tunnel/rpc"
	"gitlab.com/gitlab-org/cluster-integration/tunnel/tool/grpcz"
	"google.golang.org/grpc"
)

type server struct {
	rpc.UnsafeReverseTunnelServer
	registry *router.Registry
}

func (s *server) Connect(server grpc.BidiStreamingServer[rpc.ConnectRequest, rpc.ConnectResponse]) error {
	ctx := server.Context()
	ageCtx := grpcz.MaxConnectionAgeContextFromStreamContext(ctx)
	return s.registry.HandleTunnel(ageCtx, server)
}
