package cmd

import (
	"ctyun_lts_cli_tool/lts"
	"fmt"

	"github.com/spf13/cobra"
)

var logCmd = &cobra.Command{
	Use:   "log",
	Short: "Ctyun Log Service OpenApi",
}

var getLogsCmd = &cobra.Command{
	Use:     "getLogs",
	Short:   "Get logs from log service",
	Example: `ctlts log getLogs --project_code=<string> --unit_code=<string> --start_time=<int64> --end_time=<int64>[--query=<string> --reverse=<bool> searchType=<string> --limit=<int> --cursor=<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")
		unit_code, _ := cmd.Flags().GetString("unit_code")
		start_time, _ := cmd.Flags().GetInt64("start_time")
		end_time, _ := cmd.Flags().GetInt64("end_time")
		query, _ := cmd.Flags().GetString("query")
		reverse, _ := cmd.Flags().GetBool("reverse")
		searchType, _ := cmd.Flags().GetString("searchType")
		limit, _ := cmd.Flags().GetInt("limit")
		cursor, _ := cmd.Flags().GetString("cursor")

		lts.BuildLogCommand(cmd)

		response := lts.GetLogs(ak, sk, endpoint, project_code, unit_code, start_time, end_time, query, reverse, searchType, limit, cursor, region_id)
		fmt.Println("response:", response)
	},
}

func init() {
	// 添加子命令
	logCmd.AddCommand(getLogsCmd)

	// 添加项目创建命令的选项

	getLogsCmd.Flags().StringP("project_code", "p", "", "project_code")
	getLogsCmd.Flags().StringP("unit_code", "u", "", "unit_code")
	getLogsCmd.Flags().Int64P("start_time", "t", 0, "start_time")
	getLogsCmd.Flags().Int64P("end_time", "d", 0, "end_time")
	getLogsCmd.Flags().StringP("query", "q", "", "query")
	getLogsCmd.Flags().BoolP("reverse", "v", false, "reverse")
	getLogsCmd.Flags().StringP("searchType", "y", "forward", "searchType")
	getLogsCmd.Flags().IntP("limit", "l", 100, "limit")
	getLogsCmd.Flags().StringP("cursor", "r", "", "cursor")
	getLogsCmd.MarkFlagRequired("project_code")
	getLogsCmd.MarkFlagRequired("unit_code")
	getLogsCmd.MarkFlagRequired("start_time")
	getLogsCmd.MarkFlagRequired("end_time")

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