package exposition

import (
	"antoine-roux.tk/projects/go/pulumi-library/crds/kubernetes/externaldns/v1alpha1"
	"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"
)

const (
	loadBalancerAddressName = "internal-lb.localdomain"
	defaultRecordTTL        = 180
)

type DnsConfiguration struct {
	Name string
	Dns  string
}

func (dns *DnsConfiguration) CreateDNSRecord(
	ctx *pulumi.Context,
	namespace *v1.Namespace,
	parentApplication pulumi.Resource,
) (*v1alpha1.DNSEndpoint, error) {

	return v1alpha1.NewDNSEndpoint(ctx, fmt.Sprintf("%s-record", dns.Name), &v1alpha1.DNSEndpointArgs{
		Metadata: &v12.ObjectMetaArgs{
			Namespace: namespace.Metadata.Name(),
			Labels: pulumi.StringMap{
				"app.kubernetes.io/part-of":    pulumi.String(dns.Name),
				"app.kubernetes.io/managed-by": pulumi.String("pulumi"),
			},
		},
		Spec: &v1alpha1.DNSEndpointSpecArgs{
			Endpoints: &v1alpha1.DNSEndpointSpecEndpointsArray{
				&v1alpha1.DNSEndpointSpecEndpointsArgs{
					DnsName:    pulumi.String(dns.Dns),
					RecordTTL:  pulumi.Int(defaultRecordTTL),
					RecordType: pulumi.String("CNAME"),
					Targets: pulumi.StringArray{
						pulumi.String(loadBalancerAddressName),
					},
				},
			},
		},
	}, pulumi.Parent(parentApplication))
}
