package cmd

import (
	"ctyun_lts_cli_tool/lts"
	"fmt"

	"github.com/spf13/cobra"
)

var projectCmd = &cobra.Command{
	Use:   "project",
	Short: "Ctyun Log projects openApi",
}

var createProjectCmd = &cobra.Command{
	Use:     "createProject",
	Short:   "Create a new log project",
	Example: `ctlts project createProject --project_name=<string> [--description=<string>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_name, _ := cmd.Flags().GetString("project_name")
		description, _ := cmd.Flags().GetString("description")
		var description2 *string
		if cmd.Flags().Changed("description") { // 判断是否传入description
			description2 = &description
		} else {
			description2 = nil
		}
		lts.BuildLogCommand(cmd)
		response := lts.CreateProject(ak, sk, endpoint, project_name, description2, region_id)
		fmt.Println("response:", response)
	},
}

var deleteProjectCmd = &cobra.Command{
	Use:     "deleteProject",
	Short:   "Delete log project",
	Example: `ctlts project deleteProject --project_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_code, _ := cmd.Flags().GetString("project_code")

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

var updateProjectCmd = &cobra.Command{
	Use:     "updateProject",
	Short:   "Update log project description",
	Example: `ctlts project updateProject --project_code=<string> --description=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_code, _ := cmd.Flags().GetString("project_code")
		description, _ := cmd.Flags().GetString("description")
		// fmt.Println("description:", description)
		var description2 *string
		if cmd.Flags().Changed("description") { // 判断是否传入description
			description2 = &description
		} else {
			description2 = nil
		}
		lts.BuildLogCommand(cmd)
		response := lts.UpdateProject(ak, sk, endpoint, project_code, description2, region_id)
		fmt.Println("response:", response)
	},
}

var listProjectCmd = &cobra.Command{
	Use:     "listProject",
	Short:   "List log project all or by project name",
	Example: `ctlts project listProject [--project_name=<string>] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_name, _ := cmd.Flags().GetString("project_name")
		var project_name2 *string
		if cmd.Flags().Changed("project_name") {
			project_name2 = &project_name
		} else {
			project_name2 = nil
		}
		lts.BuildLogCommand(cmd)
		response := lts.ListProject(ak, sk, endpoint, project_name2, region_id)
		fmt.Println("response:", response)
	},
}

var GetCodeByNameCmd = &cobra.Command{
	Use:     "getCodeByName",
	Short:   "Get project code by project name",
	Example: `ctlts project getCodeByName --project_name=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_name, _ := cmd.Flags().GetString("project_name")
		lts.BuildLogCommand(cmd)
		response := lts.GetCodeByName(ak, sk, endpoint, project_name, region_id)
		fmt.Println("response:", response)

	},
}

var CheckProjectExistCmd = &cobra.Command{
	Use:     "checkProjectExist",
	Short:   "Check log project exist by project name and project code",
	Example: `ctlts project checkExist --project_name=<string> --project_code=<string> [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}
		project_name, _ := cmd.Flags().GetString("project_name")
		project_code, _ := cmd.Flags().GetString("project_code")
		lts.BuildLogCommand(cmd)
		response := lts.CheckProjectExist(ak, sk, endpoint, project_code, project_name, region_id)
		fmt.Println("response:", response)

	},
}

var CountProjectCmd = &cobra.Command{
	Use:     "countProject",
	Short:   "Count the number of log projects",
	Example: `ctlts project countProject [--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.CountProject(ak, sk, endpoint, region_id)
		fmt.Println("response:", response)
	},
}

var GetProjectCmd = &cobra.Command{
	Use:     "getProject",
	Short:   "Get log project details by project code",
	Example: `ctlts project getProject --project_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
		}

		projectCode, _ := cmd.Flags().GetString("project_code")

		// 添加日志构建步骤
		lts.BuildLogCommand(cmd)

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

var RenameProjectCmd = &cobra.Command{
	Use:     "renameProject",
	Short:   "Rename a log project by project code",
	Example: `ctlts project renameProject --project_code=<string> --alias_name=<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
		}

		projectCode, _ := cmd.Flags().GetString("project_code")
		aliasName, _ := cmd.Flags().GetString("alias_name")

		// 添加日志构建步骤
		lts.BuildLogCommand(cmd)

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

var UpdateProjectDescriptionCmd = &cobra.Command{
	Use:     "updateProjectDescription",
	Short:   "Update a log project's description by project code",
	Example: `ctlts project updateProjectDescription --project_code=<string> --description=<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
		}

		projectCode, _ := cmd.Flags().GetString("project_code")
		description, _ := cmd.Flags().GetString("description")

		lts.BuildLogCommand(cmd)

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

var GetProjectDescriptionCmd = &cobra.Command{
	Use:     "getProjectDescription",
	Short:   "Get a log project's description by project code",
	Example: `ctlts project getProjectDescription --project_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
		}

		projectCode, _ := cmd.Flags().GetString("project_code")

		lts.BuildLogCommand(cmd)

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

var GetProjectByCodeCmd = &cobra.Command{
	Use:     "getProjectByCode",
	Short:   "Get log project introduction by project code",
	Example: `ctlts project getProjectByCode --project_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
		}

		projectCode, _ := cmd.Flags().GetString("project_code")

		lts.BuildLogCommand(cmd)

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

var QueryAliasNameCmd = &cobra.Command{
	Use:     "queryAliasName",
	Short:   "Query project alias names by original project names",
	Example: `ctlts project queryAliasName --project_name_list=<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
		}

		projectNameList, _ := cmd.Flags().GetString("project_name_list")

		lts.BuildLogCommand(cmd)

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

var QueryNameByCodeCmd = &cobra.Command{
	Use:     "queryNameByCode",
	Short:   "Query original project names by project codes",
	Example: `ctlts project queryNameByCode --project_code_list=<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
		}

		projectCodeList, _ := cmd.Flags().GetString("project_code_list")

		lts.BuildLogCommand(cmd)

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

var PageProject = &cobra.Command{
	Use:     "pageProject",
	Short:   "Search log project by page",
	Example: `ctlts project pageProject --page_num=<int> --page_size=<int> [--project_name=<string> ] [--ak=<string> --sk=<string> --endpoint=<string> --region_id=<string> or --config=<string>]`,
	Run: func(cmd *cobra.Command, args []string) {
		config_name, _ := cmd.Flags().GetString("config")
		ak, sk, endpoint, region_id := lts.LoadConfig(cmd, config_name)
		params := map[string]string{
			"ak":        ak,
			"sk":        sk,
			"endpoint":  endpoint,
			"region_id": region_id,
		}
		if !lts.ValidateRequiredParams(cmd, params) {
			return
		}

		project_name, _ := cmd.Flags().GetString("project_name")
		var project_name2 *string
		if cmd.Flags().Changed("project_name") { // 判断是否传入
			project_name2 = &project_name
		} else {
			project_name2 = nil
		}
		page_num, _ := cmd.Flags().GetInt("page_num")
		page_size, _ := cmd.Flags().GetInt("page_size")
		lts.BuildLogCommand(cmd)
		response := lts.PageProject(ak, sk, endpoint, project_name2, page_num, page_size, region_id)
		fmt.Println("response:", response)
	},
}

func init() {
	// 添加子命令
	projectCmd.AddCommand(GetCodeByNameCmd)
	projectCmd.AddCommand(createProjectCmd)
	projectCmd.AddCommand(CheckProjectExistCmd)
	projectCmd.AddCommand(updateProjectCmd)
	projectCmd.AddCommand(deleteProjectCmd)
	projectCmd.AddCommand(listProjectCmd)
	projectCmd.AddCommand(QueryAliasNameCmd)
	projectCmd.AddCommand(QueryNameByCodeCmd)
	projectCmd.AddCommand(PageProject)
	projectCmd.AddCommand(GetProjectDescriptionCmd)
	projectCmd.AddCommand(GetProjectByCodeCmd)
	projectCmd.AddCommand(GetProjectCmd)
	projectCmd.AddCommand(RenameProjectCmd)
	projectCmd.AddCommand(CountProjectCmd)
	projectCmd.AddCommand(UpdateProjectDescriptionCmd)

	// 添加项目创建命令的选项
	createProjectCmd.Flags().StringP("project_name", "n", "", "Name of the project to create")
	createProjectCmd.Flags().StringP("description", "d", "", "log project description")
	createProjectCmd.MarkFlagRequired("project_name")

	GetCodeByNameCmd.Flags().StringP("project_name", "n", "", "get project_code by project_name")
	GetCodeByNameCmd.MarkFlagRequired("project_name")

	CheckProjectExistCmd.Flags().StringP("project_name", "n", "", "project_name")
	CheckProjectExistCmd.Flags().StringP("project_code", "p", "", "project_code")
	CheckProjectExistCmd.MarkFlagRequired("project_name")
	CheckProjectExistCmd.MarkFlagRequired("project_code")

	updateProjectCmd.Flags().StringP("project_code", "p", "", "project_code")
	updateProjectCmd.Flags().StringP("description", "d", "", "log project description")
	updateProjectCmd.MarkFlagRequired("project_code")

	deleteProjectCmd.Flags().StringP("project_code", "p", "", "project_code")
	deleteProjectCmd.MarkFlagRequired("project_code")

	listProjectCmd.Flags().StringP("project_name", "n", "", "project_name")

	GetProjectDescriptionCmd.Flags().StringP("project_code", "p", "", "project_code")
	GetProjectDescriptionCmd.MarkFlagRequired("project_code")

	GetProjectByCodeCmd.Flags().StringP("project_code", "p", "", "project_code")
	GetProjectByCodeCmd.MarkFlagRequired("project_code")

	GetProjectCmd.Flags().StringP("project_code", "p", "", "project_code")
	GetProjectCmd.MarkFlagRequired("project_code")

	RenameProjectCmd.Flags().StringP("project_code", "p", "", "project_code")
	RenameProjectCmd.Flags().StringP("alias_name", "n", "", "alias_name")
	RenameProjectCmd.MarkFlagRequired("project_code")
	RenameProjectCmd.MarkFlagRequired("alias_name")

	UpdateProjectDescriptionCmd.Flags().StringP("project_code", "p", "", "project_code")
	UpdateProjectDescriptionCmd.Flags().StringP("description", "d", "", "log project description")
	UpdateProjectDescriptionCmd.MarkFlagRequired("project_code")
	UpdateProjectDescriptionCmd.MarkFlagRequired("description")

	QueryAliasNameCmd.Flags().StringP("project_name_list", "n", "", "project_name_list")
	QueryAliasNameCmd.MarkFlagRequired("project_name_list")

	QueryNameByCodeCmd.Flags().StringP("project_code_list", "p", "", "project_code_list")
	QueryNameByCodeCmd.MarkFlagRequired("project_code_list")

	PageProject.Flags().StringP("project_name", "n", "", "project_name")
	PageProject.Flags().IntP("page_num", "p", 1, "page_num")
	PageProject.Flags().IntP("page_size", "z", 10, "page_size")
	PageProject.MarkFlagRequired("page_num")
	PageProject.MarkFlagRequired("page_size")

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