package cmd

import (
	"ctyun_lts_cli_tool/lts"
	"fmt"

	"github.com/spf13/cobra"
)

var instanceCmd = &cobra.Command{
	Use:   "instance",
	Short: "Ctyun Log Instance openApi",
}

var OrderLogInstanceCmd = &cobra.Command{
	Use:     "orderLogInstance",
	Short:   "Order cloud log service instance",
	Example: `ctlts instance orderLogInstance [--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
		}

		response := lts.OrderLogInstance(ak, sk, endpoint, region_id)
		fmt.Println("response:", response)
	},
}

// 定义Cobra命令用于检查订单实例状态
var CheckLicenseCmd = &cobra.Command{
	Use:     "checkLicense",
	Short:   "Check order instance status",
	Example: `ctlts instance checkLicense [--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
		}

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

var GetInstanceInfoCmd = &cobra.Command{
	Use:     "getInstanceInfo",
	Short:   "Get instance information under tenant resource pool",
	Example: `ctlts instance getInstanceInfo [--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
		}
		lts.BuildLogCommand(cmd)
		response := lts.GetInstanceInfo(ak, sk, endpoint, region_id)
		fmt.Println("response:", response)
	},
}

var GetInstanceStatusCmd = &cobra.Command{
	Use:     "getInstanceStatus",
	Short:   "Get instance status",
	Example: `ctlts instance getInstanceStatus [--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
		}

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

func init() {
	// 添加子命令
	instanceCmd.AddCommand(OrderLogInstanceCmd)
	instanceCmd.AddCommand(CheckLicenseCmd)
	instanceCmd.AddCommand(GetInstanceInfoCmd)
	instanceCmd.AddCommand(GetInstanceStatusCmd)

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