package cluster

import (
	"github.com/awslabs/eksdemo/pkg/cmd"
	"github.com/awslabs/eksdemo/pkg/eksctl"
	"github.com/awslabs/eksdemo/pkg/resource"
	"github.com/awslabs/eksdemo/pkg/resource/nodegroup"
	"github.com/awslabs/eksdemo/pkg/template"
)

func NewResource() *resource.Resource {
	resManager := &eksctl.ResourceManager{
		Resource: "cluster",
		ConfigTemplate: &template.TextTemplate{
			Template: eksctl.EksctlHeader + EksctlTemplate + nodegroup.EksctlTemplate,
		},
		DeleteFlags: []string{"--disable-nodegroup-eviction"},
	}
	res := &resource.Resource{
		Command: cmd.Command{
			Name:        "cluster",
			Description: "EKS Cluster",
			Aliases:     []string{"clusters"},
			CreateArgs:  []string{"NAME"},
			Args:        []string{"NAME"},
		},
		Getter:  &Getter{},
		Manager: resManager,
	}

	return addOptions(res, resManager)
}

const EksctlTemplate = `
addons:
- name: vpc-cni
  version: latest
  configurationValues: |-
  {{- if and (ne .KubernetesVersion "1.24") (not .DisableNetworkPolicy) }}
    enableNetworkPolicy: "true"
  {{- end }}
    env:
      ENABLE_PREFIX_DELEGATION: {{ printf "%t" .PrefixAssignment | printf "%q" }}
{{- if .IPv6 }}
- name: coredns
- name: kube-proxy
{{- end }}
{{- range .Addons }}
- name: {{ . }}
  version: latest
{{- end }}

cloudWatch:
  clusterLogging:
    enableTypes: ["*"]
{{- if .Fargate }}

fargateProfiles:
- name: default
  selectors:
    - namespace: fargate
{{- end }}
{{- if not .NoRoles }}

iam:
  withOIDC: true
  serviceAccounts:
{{- range .IrsaRoles }}
{{- $.IrsaTemplate.Render .Options }}
{{- end }}
{{- end }}
{{- if .IPv6 }}

kubernetesNetworkConfig:
  ipFamily: IPv6
{{- end }}
{{- if .Private }}

privateCluster:
  enabled: true
{{- end }}
{{- if .KMSKeyArn }}

secretsEncryption:
  keyARN: {{ .KMSKeyArn }}
{{- end }}

vpc:
  cidr: {{ .VpcCidr }}
  hostnameType: {{ .HostnameType }}

{{- if .Zones }}
availabilityZones:
{{- range .Zones }}
- {{.}}
{{- end }}
{{- end }}
`
