/*
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 deployO11yAwsRegionArgs = internal.DeployAwsRegionArgs{}

// deployO11yAwsRegionCmd represents the deploy command
var deployO11yAwsRegionCmd = &cobra.Command{
	Use:   "deploy-o11y-aws-region",
	Short: "Deploy O11y AWS region services",
	Long: `Deploy all region services for AWS. 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-o11y-aws-region --jobs-path etc/deploy-jobs/aws-region-control \
    --role arn:aws:iam::200683210682:role/auto-deploy-cli \
    --region us-west-2 \
    --k8s-config "k8s configuration" \
    --job-options "job options" \
`,
	RunE: func(cmd *cobra.Command, _ []string) error {
		deployO11yAwsRegionArgs.InstanceName = instance
		deployO11yAwsRegionArgs.OidcProviderType = oidcProvider
		metrics.AddQueuedDurationMetric()
		metrics.AddPrepareDurationMetric()
		return internal.DeployO11yAwsRegion(deployO11yAwsRegionArgs)
	},
}

func init() {
	rootCmd.AddCommand(deployO11yAwsRegionCmd)
	deployO11yAwsRegionCmd.Flags().StringVarP(&deployO11yAwsRegionArgs.JobPath, "jobs-path", "j", "etc/deploy-jobs/aws-region-control", "Batch job directory stores definition yaml file to be deployed")
	deployO11yAwsRegionCmd.Flags().StringVarP(&deployO11yAwsRegionArgs.Region, "region", "", "", "Cloud region (location) to create k8s, for example: us-west-2, eu-central-1")
	deployO11yAwsRegionCmd.Flags().StringVarP(&deployO11yAwsRegionArgs.RegionName, "logic-region-name", "n", "", "Logic region name")
	deployO11yAwsRegionCmd.Flags().StringVarP(&deployO11yAwsRegionArgs.K8s, "k8s-config", "", "", "K8s configurations")
	deployO11yAwsRegionCmd.Flags().StringVar(&deployO11yAwsRegionArgs.OutputBucket, "output-bucket", "", "Bucket to save intput and output, default is ${instance}-auto-deploy")
	deployO11yAwsRegionCmd.Flags().StringVar(&deployO11yAwsRegionArgs.RoleArn, "role", "", "AWS role arn to create k8s")
	deployO11yAwsRegionCmd.Flags().StringSliceVar(&deployO11yAwsRegionArgs.TidbClusterAccounts, "dataplane-role-arn", []string{}, "Dataplane account arns")
	deployO11yAwsRegionCmd.Flags().StringVar(&deployO11yAwsRegionArgs.ImageRegistry, "image-registry", "", "Region services and tidb cluster image registry, defult is $accountId.dkr.ecr.us-west-2.amazonaws.com")
	deployO11yAwsRegionCmd.Flags().StringVar(&deployO11yAwsRegionArgs.JobOptions, "job-options", "", "Job additional options in json format")
	deployO11yAwsRegionCmd.Flags().BoolVar(&deployO11yAwsRegionArgs.ForceOverwriteResources, "force-overwrite-resources", false, "Whether overwrite argocd secret")
	deployO11yAwsRegionCmd.Flags().StringVarP(&deployO11yAwsRegionArgs.MetaURI, "meta-uri", "", "", "Where to store or read metadata of this region")

	// mark required flag
	for _, flag := range []string{"jobs-path", "region", "logic-region-name", "k8s-config", "role", "dataplane-role-arn", "job-options", "meta-uri"} {
		deployO11yAwsRegionCmd.MarkFlagRequired(flag)
	}
}
