package ignitecmd

import (
	"github.com/spf13/cobra"
	flag "github.com/spf13/pflag"
)

const (
	flagEnableProtoVendor = "enable-proto-vendor"
)

// NewGenerate returns a command that groups code generation related sub commands.
func NewGenerate() *cobra.Command {
	c := &cobra.Command{
		Use:   "generate [command]",
		Short: "Generate clients, API docs from source code",
		Long: `Generate clients, API docs from source code.

Such as compiling protocol buffer files into Go or implement particular
functionality, for example, generating an OpenAPI spec.

Produced source code can be regenerated by running a command again and is not
meant to be edited by hand.
`,
		Aliases:           []string{"g"},
		Args:              cobra.ExactArgs(1),
		PersistentPreRunE: migrationPreRunHandler,
	}

	c.PersistentFlags().AddFlagSet(flagSetEnableProtoVendor())
	c.PersistentFlags().AddFlagSet(flagSetVerbose())

	flagSetPath(c)
	flagSetClearCache(c)

	c.AddCommand(
		NewGenerateGo(),
		NewGenerateTSClient(),
		NewGenerateComposables(),
		NewGenerateOpenAPI(),
	)

	return c
}

func flagSetEnableProtoVendor() *flag.FlagSet {
	fs := flag.NewFlagSet("", flag.ContinueOnError)
	fs.Bool(flagEnableProtoVendor, false, "enable proto package vendor for missing Buf dependencies")
	return fs
}

func flagGetEnableProtoVendor(cmd *cobra.Command) bool {
	skip, _ := cmd.Flags().GetBool(flagEnableProtoVendor)
	return skip
}
