package api

import (
	"os"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func CreateGrpcConnection() (*grpc.ClientConn, error) {
	var opts []grpc.DialOption
	opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))

	// get grpc connection url from env, default to localhost:50051
	grpcConnectionUrl := os.Getenv("AI_SERVICES_GRPC_URL")
	if grpcConnectionUrl == "" {
		grpcConnectionUrl = "localhost:50051"
	}

	conn, err := grpc.NewClient(grpcConnectionUrl, opts...)
	if err != nil {
		return nil, err
	}

	return conn, nil
}
