package cmd

import (
	"ctyun_lts_cli_tool/lts"
	"fmt"

	"github.com/spf13/cobra"
)

var dataDownloadCmd = &cobra.Command{
	Use:   "dataDownload",
	Short: "Ctyun Log Service Data Download openApi",
}

var GetSignUrlDownloadCmd = &cobra.Command{
	Use:     "getSignUrl",
	Short:   "Get the download link for the specified download task",
	Example: `ctlts dataDownload getSignUrl --task_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		configName, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		task_code, _ := cmd.Flags().GetString("task_code")
		response := lts.GetSignUrlDownload(ak, sk, endpoint, task_code, region_id)
		fmt.Println("response:", response)
	},
}

var QueryDownloadCmd = &cobra.Command{
	Use:     "query",
	Short:   "Get the download message for the specified download task.",
	Example: `ctlts dataDownload query --task_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {

		configName, _ := cmd.Flags().GetString("config")

		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)

		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}

		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		task_code, _ := cmd.Flags().GetString("task_code")

		lts.BuildLogCommand(cmd)
		response := lts.QueryDownload(ak, sk, endpoint, task_code, region_id)
		fmt.Println("response:", response)
	},
}

var DeleteDownloadCmd = &cobra.Command{
	Use:     "delete",
	Short:   "Delete download task",
	Example: `ctlts dataDownload delete --task_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {

		configName, _ := cmd.Flags().GetString("config")

		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)

		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}

		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		task_code, _ := cmd.Flags().GetString("task_code")

		lts.BuildLogCommand(cmd)
		response := lts.DeleteDownload(ak, sk, endpoint, task_code, region_id)
		fmt.Println("response:", response)
	},
}

var ListTaskDownloadCmd = &cobra.Command{
	Use:     "list",
	Short:   "List download task",
	Example: `ctlts dataDownload list --unit_code=<string> [--page_num=<int> --page_size=<int>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {

		configName, _ := cmd.Flags().GetString("config")

		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)

		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}

		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unit_code, _ := cmd.Flags().GetString("unit_code")
		page_size, _ := cmd.Flags().GetInt("page_size")
		page_num, _ := cmd.Flags().GetInt("page_num")

		lts.BuildLogCommand(cmd)
		response := lts.ListTaskDownload(ak, sk, endpoint, unit_code, page_size, page_num, region_id)
		fmt.Println("response:", response)
	},
}

var CreateDownloadCmd = &cobra.Command{
	Use:     "create",
	Short:   "Create download task",
	Example: `ctlts dataDownload create --unit_code=<string> --from=<int64> --to=<int64> --log_num=<int> [--query=<string> --format=<string> --reverse=<bool>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {

		configName, _ := cmd.Flags().GetString("config")

		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, configName)

		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}

		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		unit_code, _ := cmd.Flags().GetString("unit_code")
		from, _ := cmd.Flags().GetInt64("from")
		to, _ := cmd.Flags().GetInt64("to")
		query, _ := cmd.Flags().GetString("query")
		reverse, _ := cmd.Flags().GetBool("reverse")
		customLogNum, _ := cmd.Flags().GetInt("log_num")
		format, _ := cmd.Flags().GetString("format")
		lts.BuildLogCommand(cmd)
		response := lts.CreateDownload(ak, sk, endpoint, unit_code, from, to, customLogNum, query, format, reverse, region_id)
		fmt.Println("response:", response)
	},
}

func init() {
	// 添加子命令
	dataDownloadCmd.AddCommand(CreateDownloadCmd)
	dataDownloadCmd.AddCommand(DeleteDownloadCmd)
	dataDownloadCmd.AddCommand(GetSignUrlDownloadCmd)
	dataDownloadCmd.AddCommand(ListTaskDownloadCmd)
	dataDownloadCmd.AddCommand(QueryDownloadCmd)

	DeleteDownloadCmd.Flags().StringP("task_code", "t", "", "task_code")
	DeleteDownloadCmd.MarkFlagRequired("task_code")

	GetSignUrlDownloadCmd.Flags().StringP("task_code", "t", "", "task_code")
	GetSignUrlDownloadCmd.MarkFlagRequired("task_code")

	QueryDownloadCmd.Flags().StringP("task_code", "t", "", "task_code")
	QueryDownloadCmd.MarkFlagRequired("task_code")

	ListTaskDownloadCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	ListTaskDownloadCmd.Flags().IntP("page_num", "n", 1, "page_num")
	ListTaskDownloadCmd.Flags().IntP("page_size", "z", 10, "page_size")
	ListTaskDownloadCmd.MarkFlagRequired("unit_code")

	CreateDownloadCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	CreateDownloadCmd.Flags().Int64P("from", "f", 0, "from time [s]")
	CreateDownloadCmd.Flags().Int64P("to", "t", 0, "to time [s]")
	CreateDownloadCmd.Flags().IntP("log_num", "l", 500, "number of downloaded logs")
	CreateDownloadCmd.Flags().StringP("query", "q", "", "query statement")
	CreateDownloadCmd.Flags().StringP("format", "m", "json", "data type")
	CreateDownloadCmd.Flags().BoolP("reverse", "r", false, " sort reverse")
	CreateDownloadCmd.MarkFlagRequired("unit_code")
	CreateDownloadCmd.MarkFlagRequired("from")
	CreateDownloadCmd.MarkFlagRequired("to")

	// 添加全局选项
	dataDownloadCmd.PersistentFlags().StringP("ak", "a", "", "access_id")
	dataDownloadCmd.PersistentFlags().StringP("sk", "s", "", "access_key")
	dataDownloadCmd.PersistentFlags().StringP("endpoint", "e", "", "endpoint")
	dataDownloadCmd.PersistentFlags().StringP("region_id", "i", "", "region_id")
	dataDownloadCmd.PersistentFlags().StringP("config", "c", "", "use config.yaml ,such as: --config=huadong1")
	rootCmd.AddCommand(dataDownloadCmd)
}
