package cli

import (
	"github.com/spf13/cobra"

	"github.com/tidbcloud/observability/vm-migration/internal/tenants"
)

// newTenantsDedicatedCmd returns the tenants dedicated command group: one
// subcommand per discovery system.
//
// Run each subcommand in an environment that reaches its system; combine the
// outputs (and other sources) with tenants merge.
func newTenantsDedicatedCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "dedicated",
		Short: "Discover dedicated tenants (k8s, s3)",
		Long: `Discovers dedicated tenants, one system per subcommand:
  k8s  kubectl get clusters -A -l <dedicated biz selector> -o json —
       live clusters
  s3   aws s3 cp --recursive archived/<bizType>/**/cluster.yaml bulk
       download, kept when modified within --max-age — archived clusters

Both outputs are tenants JSONs; tenants merge combines them and prefers
archive entries over k8s on tenant collisions.`,
		Args:         cobra.NoArgs,
		SilenceUsage: true,
	}
	cmd.AddCommand(newTenantsDedicatedK8sCmd(), newTenantsDedicatedS3Cmd())
	return cmd
}

// newTenantsDedicatedK8sCmd returns the k8s discovery subcommand.
func newTenantsDedicatedK8sCmd() *cobra.Command {
	var o tenants.DedicatedK8sOptions
	cmd := &cobra.Command{
		Use:   "k8s",
		Short: "Discover dedicated tenants from live Kubernetes clusters",
		Long: `Runs kubectl get clusters -A -l <dedicated biz selector> -o json in
the current kubectl context and writes the tenants JSON for the live
dedicated clusters.`,
		Example: `  vm-migration tenants dedicated k8s \
    --output tenants-dedicated-k8s.json`,
		Args:         cobra.NoArgs,
		SilenceUsage: true,
		RunE: func(cmd *cobra.Command, args []string) error {
			return tenants.RunDedicatedK8s(
				cmd.Context(),
				o,
				tenants.NewKubectlShellLister(),
				cmd.OutOrStdout(),
				cmd.ErrOrStderr(),
			)
		},
	}
	cmd.Flags().StringVar(&o.Output, "output", "", "Output tenants JSON path (default: stdout)")
	return cmd
}

// newTenantsDedicatedS3Cmd returns the S3 archive discovery subcommand.
func newTenantsDedicatedS3Cmd() *cobra.Command {
	var o tenants.DedicatedS3Options
	o.MaxAge = tenants.DefaultMaxAge
	cmd := &cobra.Command{
		Use:   "s3",
		Short: "Discover dedicated tenants from the S3 archive",
		Long: `Bulk-downloads every cluster.yaml under the bucket's
archived/<bizType>/ prefixes (aws s3 cp --recursive, transfer output
streamed live) and writes the tenants JSON for clusters whose archive file
was modified within --max-age.`,
		Example: `  vm-migration tenants dedicated s3 \
    --s3-bucket <bucket> \
    --output tenants-dedicated-s3.json`,
		Args:         cobra.NoArgs,
		SilenceUsage: true,
		RunE: func(cmd *cobra.Command, args []string) error {
			return tenants.RunDedicatedS3(
				cmd.Context(),
				o,
				tenants.NewAWSLister(),
				tenants.NewAWSDownloader(cmd.ErrOrStderr()),
				cmd.OutOrStdout(),
				cmd.ErrOrStderr(),
			)
		},
	}
	cmd.Flags().StringVar(&o.S3Bucket, "s3-bucket", "", "S3 bucket holding the archived/ tree (required)")
	cmd.Flags().StringVar(&o.MaxAge, "max-age", o.MaxAge, "Only keep archive files modified within last N days (e.g. 30d)")
	cmd.Flags().StringVar(&o.Output, "output", "", "Output tenants JSON path (default: stdout)")
	mustMarkRequired(cmd, "s3-bucket")
	return cmd
}
