/*
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 deployAzureO11yRegionArgs = internal.DeployAzureRegionArgs{}

// deployAzureO11yRegionCmd represents the deploy command
var deployAzureO11yRegionCmd = &cobra.Command{
	Use:   "deploy-azure-o11y-region",
	Short: "Deploy Azure O11y 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                               \
   --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 {
		deployAzureO11yRegionArgs.InstanceName = instance
		deployAzureO11yRegionArgs.OidcProviderType = oidcProvider
		metrics.AddQueuedDurationMetric()
		metrics.AddPrepareDurationMetric()
		return internal.DeployAzureO11yRegion(deployAzureO11yRegionArgs)
	},
}

func init() {
	rootCmd.AddCommand(deployAzureO11yRegionCmd)
	deployAzureO11yRegionCmd.Flags().StringVarP(&deployAzureO11yRegionArgs.JobPath, "jobs-path", "j", "etc/deploy-jobs/azure-region-control", "Batch job directory stores definition yaml file to be deployed")
	deployAzureO11yRegionCmd.Flags().StringVarP(&deployAzureO11yRegionArgs.Region, "cloud-region", "r", "", "Cloud region (location) to create k8s, for example: eastus, westus")
	deployAzureO11yRegionCmd.Flags().StringVarP(&deployAzureO11yRegionArgs.RegionName, "logic-region-name", "n", "", "Logic region name")
	deployAzureO11yRegionCmd.Flags().StringVarP(&deployAzureO11yRegionArgs.K8s, "k8s-config", "", "", "K8s configurations in yaml format")
	deployAzureO11yRegionCmd.Flags().StringVar(&deployAzureO11yRegionArgs.OutputBlobEndpoint, "output-blob-endpoint", "", "Bucket to save intput and output")
	deployAzureO11yRegionCmd.Flags().StringVar(&deployAzureO11yRegionArgs.TenantId, "tenant-id", "", "Tenant id to create k8s")
	deployAzureO11yRegionCmd.Flags().StringVar(&deployAzureO11yRegionArgs.SubscriptionId, "subscription-id", "", "Subscription id to create k8s")
	deployAzureO11yRegionCmd.Flags().StringVar(&deployAzureO11yRegionArgs.ClientId, "client-id", "", "Client id to create k8s")
	deployAzureO11yRegionCmd.Flags().StringVar(&deployAzureO11yRegionArgs.ImageRegistry, "image-registry", "", "Region services and tidb cluster image registry")
	deployAzureO11yRegionCmd.Flags().StringVar(&deployAzureO11yRegionArgs.JobOptions, "job-options", "", "Job additional options in yaml format")
	deployAzureO11yRegionCmd.Flags().StringVar(&deployAzureO11yRegionArgs.GlobalOptions, "global-options", "", "The global options in yaml format")
	deployAzureO11yRegionCmd.Flags().BoolVar(&deployAzureO11yRegionArgs.ForceOverwriteResources, "force-overwrite-resources", false, "Whether overwrite argocd secret")
	deployAzureO11yRegionCmd.Flags().StringVarP(&deployAzureO11yRegionArgs.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", "logic-region-name", "k8s-config", "output-blob-endpoint", "terraform-state-blob-endpoint", "tenant-id", "subscription-id", "client-id", "image-registry", "job-options", "meta-uri"} {
		deployAzureO11yRegionCmd.MarkFlagRequired(flag)
	}
}
