// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT.

package custom_app_integration

import (
	"fmt"

	"github.com/databricks/cli/cmd/root"
	"github.com/databricks/cli/libs/cmdio"
	"github.com/databricks/cli/libs/flags"
	"github.com/databricks/databricks-sdk-go/service/oauth2"
	"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
	Use:   "custom-app-integration",
	Short: `These APIs enable administrators to manage custom oauth app integrations, which is required for adding/using Custom OAuth App Integration like Tableau Cloud for Databricks in AWS cloud.`,
	Long: `These APIs enable administrators to manage custom oauth app integrations,
  which is required for adding/using Custom OAuth App Integration like Tableau
  Cloud for Databricks in AWS cloud.
  
  **Note:** You can only add/use the OAuth custom application integrations when
  OAuth enrollment status is enabled. For more details see
  :method:OAuthEnrollment/create`,
	Annotations: map[string]string{
		"package": "oauth2",
	},
}

// start create command
var createReq oauth2.CreateCustomAppIntegration
var createJson flags.JsonFlag

func init() {
	Cmd.AddCommand(createCmd)
	// TODO: short flags
	createCmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`)

	createCmd.Flags().BoolVar(&createReq.Confidential, "confidential", createReq.Confidential, `indicates if an oauth client-secret should be generated.`)
	// TODO: complex arg: token_access_policy

}

var createCmd = &cobra.Command{
	Use:   "create",
	Short: `Create Custom OAuth App Integration.`,
	Long: `Create Custom OAuth App Integration.
  
  Create Custom OAuth App Integration.
  
  You can retrieve the custom oauth app integration via
  :method:CustomAppIntegration/get.`,

	Annotations: map[string]string{},
	PreRunE:     root.MustAccountClient,
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx := cmd.Context()
		a := root.AccountClient(ctx)

		if cmd.Flags().Changed("json") {
			err = createJson.Unmarshal(&createReq)
			if err != nil {
				return err
			}
		} else {
			return fmt.Errorf("please provide command input in JSON format by specifying the --json flag")
		}

		response, err := a.CustomAppIntegration.Create(ctx, createReq)
		if err != nil {
			return err
		}
		return cmdio.Render(ctx, response)
	},
	// Disable completions since they are not applicable.
	// Can be overridden by manual implementation in `override.go`.
	ValidArgsFunction: cobra.NoFileCompletions,
}

// start delete command
var deleteReq oauth2.DeleteCustomAppIntegrationRequest

func init() {
	Cmd.AddCommand(deleteCmd)
	// TODO: short flags

}

var deleteCmd = &cobra.Command{
	Use:   "delete INTEGRATION_ID",
	Short: `Delete Custom OAuth App Integration.`,
	Long: `Delete Custom OAuth App Integration.
  
  Delete an existing Custom OAuth App Integration. You can retrieve the custom
  oauth app integration via :method:CustomAppIntegration/get.`,

	Annotations: map[string]string{},
	Args: func(cmd *cobra.Command, args []string) error {
		check := cobra.ExactArgs(1)
		return check(cmd, args)
	},
	PreRunE: root.MustAccountClient,
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx := cmd.Context()
		a := root.AccountClient(ctx)

		deleteReq.IntegrationId = args[0]

		err = a.CustomAppIntegration.Delete(ctx, deleteReq)
		if err != nil {
			return err
		}
		return nil
	},
	// Disable completions since they are not applicable.
	// Can be overridden by manual implementation in `override.go`.
	ValidArgsFunction: cobra.NoFileCompletions,
}

// start get command
var getReq oauth2.GetCustomAppIntegrationRequest

func init() {
	Cmd.AddCommand(getCmd)
	// TODO: short flags

}

var getCmd = &cobra.Command{
	Use:   "get INTEGRATION_ID",
	Short: `Get OAuth Custom App Integration.`,
	Long: `Get OAuth Custom App Integration.
  
  Gets the Custom OAuth App Integration for the given integration id.`,

	Annotations: map[string]string{},
	Args: func(cmd *cobra.Command, args []string) error {
		check := cobra.ExactArgs(1)
		return check(cmd, args)
	},
	PreRunE: root.MustAccountClient,
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx := cmd.Context()
		a := root.AccountClient(ctx)

		getReq.IntegrationId = args[0]

		response, err := a.CustomAppIntegration.Get(ctx, getReq)
		if err != nil {
			return err
		}
		return cmdio.Render(ctx, response)
	},
	// Disable completions since they are not applicable.
	// Can be overridden by manual implementation in `override.go`.
	ValidArgsFunction: cobra.NoFileCompletions,
}

// start list command

func init() {
	Cmd.AddCommand(listCmd)

}

var listCmd = &cobra.Command{
	Use:   "list",
	Short: `Get custom oauth app integrations.`,
	Long: `Get custom oauth app integrations.
  
  Get the list of custom oauth app integrations for the specified Databricks
  account`,

	Annotations: map[string]string{},
	PreRunE:     root.MustAccountClient,
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx := cmd.Context()
		a := root.AccountClient(ctx)
		response, err := a.CustomAppIntegration.ListAll(ctx)
		if err != nil {
			return err
		}
		return cmdio.Render(ctx, response)
	},
	// Disable completions since they are not applicable.
	// Can be overridden by manual implementation in `override.go`.
	ValidArgsFunction: cobra.NoFileCompletions,
}

// start update command
var updateReq oauth2.UpdateCustomAppIntegration
var updateJson flags.JsonFlag

func init() {
	Cmd.AddCommand(updateCmd)
	// TODO: short flags
	updateCmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`)

	// TODO: array: redirect_urls
	// TODO: complex arg: token_access_policy

}

var updateCmd = &cobra.Command{
	Use:   "update INTEGRATION_ID",
	Short: `Updates Custom OAuth App Integration.`,
	Long: `Updates Custom OAuth App Integration.
  
  Updates an existing custom OAuth App Integration. You can retrieve the custom
  oauth app integration via :method:CustomAppIntegration/get.`,

	Annotations: map[string]string{},
	Args: func(cmd *cobra.Command, args []string) error {
		check := cobra.ExactArgs(1)
		return check(cmd, args)
	},
	PreRunE: root.MustAccountClient,
	RunE: func(cmd *cobra.Command, args []string) (err error) {
		ctx := cmd.Context()
		a := root.AccountClient(ctx)

		if cmd.Flags().Changed("json") {
			err = updateJson.Unmarshal(&updateReq)
			if err != nil {
				return err
			}
		}
		updateReq.IntegrationId = args[0]

		err = a.CustomAppIntegration.Update(ctx, updateReq)
		if err != nil {
			return err
		}
		return nil
	},
	// Disable completions since they are not applicable.
	// Can be overridden by manual implementation in `override.go`.
	ValidArgsFunction: cobra.NoFileCompletions,
}

// end service CustomAppIntegration