/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"github.com/spf13/cobra"
	"github.com/tidbcloud/auto-deploy/internal"
	"github.com/tidbcloud/auto-deploy/internal/metrics"
)

var deployAzureRegionArgs = internal.DeployAzureRegionArgs{}

// deployAzureRegionCmd represents the deploy command
var deployAzureRegionCmd = &cobra.Command{
	Use:   "deploy-azure-region",
	Short: "Deploy Azure region services",
	Long: `Deploy all region services for Azure. The services is deployed by a deploy job.
All jobs is located in 'jobs-path' folder, And the deploy job can define the 
dependent job via annotation:
    tidbcloud.com/depends-on: cluster-service
And we will run the jobs as unspeced order, but if the job has dependent job,
we will run the dependent job first. If the dependent has also new dependent
job, the new dependent job also be run before dependent job.

For example:
deploy-cli deploy-azure-region --jobs-path etc/deploy-jobs/azure-region-control                               \
   --region-name eastus-test1                                                                                 \
   --cloud-name eastus                                                                                        \
   --controlplane-tenant-id 6c865c34-8ff8-4792-bbca-8c9d15efb29d                                              \
   --controlplane-subscription-id 85b598b9-a080-4476-bc96-ba89e64f92d1                                        \
   --controlplane-client-id 920139c4-dd78-48f2-82bc-fba45b872acb                                              \
   --k8s-config "k8s configuration"                                                                           \
   --job-options "job options"                                                                                \
   --meta-uri "https://devautodeploymeta.blob.core.windows.net/meta/dedicated-regions/eastus-test1/meta.yaml" \
   --image-registry tidbclouddev.azurecr.io                                                                    \
   --tenant-id 6c865c34-8ff8-4792-bbca-8c9d15efb29d                                                           \
   --subscription-id 85b598b9-a080-4476-bc96-ba89e64f92d1                                                     \
   --client-id 6c865c34-8ff8-4792-bbca-8c9d15efb29d                                                           \
`,
	RunE: func(cmd *cobra.Command, _ []string) error {
		deployAzureRegionArgs.InstanceName = instance
		deployAzureRegionArgs.OidcProviderType = oidcProvider
		metrics.AddQueuedDurationMetric()
		metrics.AddPrepareDurationMetric()
		return internal.DeployAzureRegion(deployAzureRegionArgs)
	},
}

func init() {
	rootCmd.AddCommand(deployAzureRegionCmd)
	deployAzureRegionCmd.Flags().StringVarP(&deployAzureRegionArgs.JobPath, "jobs-path", "j", "etc/deploy-jobs/azure-region-control", "Batch job directory stores definition yaml file to be deployed")
	deployAzureRegionCmd.Flags().StringVarP(&deployAzureRegionArgs.RegionName, "region-name", "n", "", "Region name")
	deployAzureRegionCmd.Flags().StringVarP(&deployAzureRegionArgs.Region, "cloud-region", "r", "", "Cloud region (location) to create k8s, for example: eastus, westus")
	deployAzureRegionCmd.Flags().StringVarP(&deployAzureRegionArgs.K8s, "k8s-config", "", "", "K8s configurations in yaml format")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.OutputBlobEndpoint, "output-blob-endpoint", "", "Bucket to save intput and output")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.TerraformStateBlobEndpoint, "terraform-state-blob-endpoint", "", "Bucket to save terraform state")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.TenantId, "tenant-id", "", "Tenant id to create k8s")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.SubscriptionId, "subscription-id", "", "Subscription id to create k8s")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.ClientId, "client-id", "", "Client id to create k8s")
	deployAzureRegionCmd.Flags().StringSliceVar(&deployAzureRegionArgs.TidbClusterAccounts, "dataplane-subscription-ids", []string{}, "Dataplane account subscription id list")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.ImageRegistry, "image-registry", "", "Region services and tidb cluster image registry")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.JobOptions, "job-options", "", "Job additional options in yaml format")
	deployAzureRegionCmd.Flags().StringVar(&deployAzureRegionArgs.GlobalOptions, "global-options", "", "The global options in yaml format")
	deployAzureRegionCmd.Flags().BoolVar(&deployAzureRegionArgs.ForceOverwriteResources, "force-overwrite-resources", false, "Whether overwrite argocd secret")
	deployAzureRegionCmd.Flags().StringVarP(&deployAzureRegionArgs.MetaURI, "meta-uri", "", "", "Where to store or read metadata of this region")

	// mark required flag
	for _, flag := range []string{"jobs-path", "region-name", "cloud-region", "k8s-config", "output-blob-endpoint", "terraform-state-blob-endpoint", "tenant-id", "subscription-id", "client-id", "image-registry", "job-options", "meta-uri"} {
		deployAzureRegionCmd.MarkFlagRequired(flag)
	}
}
