package exposition

import (
	"fmt"
	"github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/core/v1"
	v12 "github.com/pulumi/pulumi-kubernetes/sdk/v4/go/kubernetes/meta/v1"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

type ServiceConfiguration struct {
	Name string
}

func (service *ServiceConfiguration) CreateService(
	ctx *pulumi.Context,
	namespace *v1.Namespace,
	parentApplication pulumi.Resource,
	appLabels pulumi.StringMap,
) (*v1.Service, error) {
	return v1.NewService(ctx, fmt.Sprintf("%s-service", service.Name), &v1.ServiceArgs{
		Metadata: &v12.ObjectMetaArgs{
			Namespace: namespace.Metadata.Name(),
			Labels: pulumi.StringMap{
				"app.kubernetes.io/part-of":    pulumi.String(service.Name),
				"app.kubernetes.io/managed-by": pulumi.String("pulumi"),
			},
		},
		Spec: &v1.ServiceSpecArgs{
			Type:     pulumi.String("ClusterIP"),
			Selector: appLabels,
			Ports: v1.ServicePortArray{
				v1.ServicePortArgs{
					Name:       pulumi.String("exposed-port"),
					Port:       pulumi.Int(8090),
					TargetPort: pulumi.String("http"),
					Protocol:   pulumi.String("TCP"),
				},
			},
		},
	}, pulumi.Parent(parentApplication))
}
