apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  name: helm
spec:
  workload:
    type: autodetects.core.oam.dev
  schematic:
    cue:
      template: |
        output: {
        	apiVersion: "source.toolkit.fluxcd.io/v1beta1"
        	metadata: {
        		name: context.name
        	}
        	if parameter.repoType == "git" {
        		kind: "GitRepository"
        		spec: {
        			url: parameter.url
        			if parameter.git.branch != _|_ {
        				ref: branch: parameter.git.branch
        			}
        			_secret
        			_sourceCommonArgs
        		}
        	}
        	if parameter.repoType == "oss" {
        		kind: "Bucket"
        		spec: {
        			endpoint:   parameter.url
        			bucketName: parameter.oss.bucketName
        			provider:   parameter.oss.provider
        			if parameter.oss.region != _|_ {
        				region: parameter.oss.region
        			}
        			_secret
        			_sourceCommonArgs
        		}
        	}
        	if parameter.repoType == "helm" {
        		kind: "HelmRepository"
        		spec: {
        			url: parameter.url
        			_secret
        			_sourceCommonArgs
        		}
        	}
        }

        outputs: release: {
        	apiVersion: "helm.toolkit.fluxcd.io/v2beta1"
        	kind:       "HelmRelease"
        	metadata: {
        		name: context.name
        	}
        	spec: {
        		interval: parameter.pullInterval
        		chart: {
        			spec: {
        				chart:   parameter.chart
        				version: parameter.version
        				sourceRef: {
        					if parameter.repoType == "git" {
        						kind: "GitRepository"
        					}
        					if parameter.repoType == "helm" {
        						kind: "HelmRepository"
        					}
        					if parameter.repoType == "oss" {
        						kind: "Bucket"
        					}
        					name:      context.name
        					namespace: context.namespace
        				}
        				interval: parameter.pullInterval
        			}
        		}
        		if parameter.targetNamespace != _|_ {
        			targetNamespace: parameter.targetNamespace
        		}
        		if parameter.releaseName != _|_ {
        			releaseName: parameter.releaseName
        		}
        		if parameter.values != _|_ {
        			values: parameter.values
        		}
        	}
        }

        _secret: {
        	if parameter.secretRef != _|_ {
        		secretRef: {
        			name: parameter.secretRef
        		}
        	}
        }

        _sourceCommonArgs: {
        	interval: parameter.pullInterval
        	if parameter.timeout != _|_ {
        		timeout: parameter.timeout
        	}
        }

        parameter: {
        	repoType: *"helm" | "git" | "oss"
        	// +usage=The interval at which to check for repository/bucket and relese updates, default to 5m
        	pullInterval: *"5m" | string
        	// +usage=The Git or Helm repository URL, OSS endpoint, accept HTTP/S or SSH address as git url,
        	url: string
        	// +usage=The name of the secret containing authentication credentials
        	secretRef?: string
        	// +usage=The timeout for operations like download index/clone repository, optional
        	timeout?: string

        	git?: {
        		// +usage=The Git reference to checkout and monitor for changes, defaults to master branch
        		branch: string
        	}
        	oss?: {
        		// +usage=The bucket's name, required if repoType is oss
        		bucketName: string
        		// +usage="generic" for Minio, Amazon S3, Google Cloud Storage, Alibaba Cloud OSS, "aws" for retrieve credentials from the EC2 service when credentials not specified, default "generic"
        		provider: *"generic" | "aws"
        		// +usage=The bucket region, optional
        		region?: string
        	}

        	// +usage=1.The relative path to helm chart for git/oss source. 2. chart name for helm resource 3. relative path for chart package(e.g. ./charts/podinfo-1.2.3.tgz)
        	chart: string
        	// +usage=Chart version
        	version: *"*" | string
        	// +usage=The namespace for helm chart, optional
        	targetNamespace?: string
        	// +usage=The release name
        	releaseName?: string
        	// +usage=Chart values
        	values?: #nestedmap
        }

        #nestedmap: {
        	...
        }