package utils_test

import (
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/tidbcloud/auto-deploy/internal/constants"
	"github.com/tidbcloud/auto-deploy/internal/types"
	"github.com/tidbcloud/auto-deploy/internal/utils"
)

func TestRenderJobOptionsWithFunctions(t *testing.T) {
	yamlOptions := `
` + constants.JOB_TEMPLATE_JOB_OPTIONS + `:
  regionalServer:
    dnsName: ap-northeast-1-test1.regional-api.dev.aws.tidbcloud.com
    hostedZone: dev.aws.tidbcloud.com
    hostedZoneId: '{{ getHostedZoneId "dev.aws.tidbcloud.com" }}'
    loadBalancerHostedZoneId: '{{ getLoadBalancerHostedZoneId }}'
    availabilityZones: '{{ getAvailabilityZonesN 3 }}'
    publicHostedZoneDomain: clusters.dev.tidb-cloud.com
    publicHostedZoneID: '{{ getHostedZoneId "clusters.dev.tidb-cloud.com" }}'
    defaultDataplaneAccount: "385595570414"
    auth0Audience: dev-proxy-service
    auth0Domain: tidb-dev.us.auth0.com
`
	temp, err := utils.RenderJobOptionsTemplate(string(yamlOptions), &types.Region{
		Region: "us-west-2",
	})
	assert.NoError(t, err)
	_ = temp
}
func TestRenderK8sTemplateWithFunctions(t *testing.T) {
	os.Chdir(os.Getenv("WORKSPACE_DIR"))
	yamlOptions := `
version: "1.30"
template-path: etc/k8s-templates/eks-v1.yaml
admin-roles:
  - cicd-role
  - argocd-oidc-role
  - full-manager-service-role
  - dbaas-dev
  - dbaas-dev-readonly
  - dbaas-central-role-dev
vpc:
  cidr: 10.1.0.0/16
  clusterEndpoints:
    privateAccess: true
    publicAccess: true
  nat:
    gateway: Single
  publicAccessCIDRs:
    - 0.0.0.0/0
node-groups:
  - amiFamily: Bottlerocket
    availabilityZones:
      - '{{ getAvailabilityZoneIndex 0 }}'
    desiredCapacity: 1
    instanceType: m5.large
    maxPodsPerNode: 110
    maxSize: 10
    name: worker
tags:
  servicetype: dedicated
  usedby: dbaas-control-plane-seed
addons:
  - name: vpc-cni
    version: latest
    attachPolicyARNs:
      - arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy
    configurationValues: |
      env:
        ENABLE_PREFIX_DELEGATION: "true"
  - name: coredns
    version: '{{ getEksAddonLatestVersion "coredns" "1.30" }}'
  - name: kube-proxy
    version: latest
  - name: aws-ebs-csi-driver
    version: '{{ getEksAddonDefaultVersion "aws-ebs-csi-driver" "1.30" }}'
    wellKnownPolicies:
      ebsCSIController: true
`
	temp, err := utils.RenderK8sTemplate(string(yamlOptions), &types.Region{
		Region: "us-west-2",
	})
	assert.NoError(t, err)
	_ = temp
}
