/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"encoding/json"

	log "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"mortons.site/tps/internal/client"
	"mortons.site/tps/internal/properties"
)

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
	Use:    "delete [name]",
	Short:  "Deletes a property",
	Long:   `Deletes and existing property`,
	PreRun: toggleDebug,
	Args:   cobra.ExactArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		p := []properties.PropertyDelete{{Type: "vcfg:PropertyDelete", Name: args[0]}}
		pJSON, err := json.Marshal(p)
		if err != nil {
			return err
		}
		log.Debug(string(pJSON))

		hostname := viper.GetString("hostname")
		username := viper.GetString("username")
		password := viper.GetString("password")
		clientId := viper.GetString("clientId")

		err = client.DeleteProperty(&client.Config{Hostname: hostname, Username: username, Password: password, ClientID: clientId}, p)

		if err != nil {
			return err
		}
		return nil
	},
}

func init() {
	rootCmd.AddCommand(deleteCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
