package frostfsid

import (
	"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags"
	commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

var (
	frostfsidAddSubjectKeyCmd = &cobra.Command{
		Use:   "add-subject-key",
		Short: "Add a public key to the subject in frostfsid contract",
		PreRun: func(cmd *cobra.Command, _ []string) {
			_ = viper.BindPFlag(commonflags.AlphabetWalletsFlag, cmd.Flags().Lookup(commonflags.AlphabetWalletsFlag))
			_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
		},
		Run: frostfsidAddSubjectKey,
	}
	frostfsidRemoveSubjectKeyCmd = &cobra.Command{
		Use:   "remove-subject-key",
		Short: "Remove a public key from the subject in frostfsid contract",
		PreRun: func(cmd *cobra.Command, _ []string) {
			_ = viper.BindPFlag(commonflags.AlphabetWalletsFlag, cmd.Flags().Lookup(commonflags.AlphabetWalletsFlag))
			_ = viper.BindPFlag(commonflags.EndpointFlag, cmd.Flags().Lookup(commonflags.EndpointFlag))
		},
		Run: frostfsidRemoveSubjectKey,
	}
)

func initFrostfsIDAddSubjectKeyCmd() {
	Cmd.AddCommand(frostfsidAddSubjectKeyCmd)

	ff := frostfsidAddSubjectKeyCmd.Flags()
	ff.StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
	ff.String(commonflags.AlphabetWalletsFlag, "", commonflags.AlphabetWalletsFlagDesc)

	ff.String(subjectAddressFlag, "", "Subject address")
	_ = frostfsidAddSubjectKeyCmd.MarkFlagRequired(subjectAddressFlag)

	ff.String(subjectKeyFlag, "", "Public key to add")
	_ = frostfsidAddSubjectKeyCmd.MarkFlagRequired(subjectKeyFlag)
}

func initFrostfsIDRemoveSubjectKeyCmd() {
	Cmd.AddCommand(frostfsidRemoveSubjectKeyCmd)

	ff := frostfsidRemoveSubjectKeyCmd.Flags()
	ff.StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
	ff.String(commonflags.AlphabetWalletsFlag, "", commonflags.AlphabetWalletsFlagDesc)

	ff.String(subjectAddressFlag, "", "Subject address")
	_ = frostfsidAddSubjectKeyCmd.MarkFlagRequired(subjectAddressFlag)

	ff.String(subjectKeyFlag, "", "Public key to remove")
	_ = frostfsidAddSubjectKeyCmd.MarkFlagRequired(subjectKeyFlag)
}

func frostfsidAddSubjectKey(cmd *cobra.Command, _ []string) {
	addr := getFrostfsIDSubjectAddress(cmd)
	pub := getFrostfsIDSubjectKey(cmd)

	ffsid, err := newFrostfsIDClient(cmd)
	commonCmd.ExitOnErr(cmd, "init contract client: %w", err)

	ffsid.addCall(ffsid.roCli.AddSubjectKeyCall(addr, pub))

	err = ffsid.sendWait()
	commonCmd.ExitOnErr(cmd, "add subject key: %w", err)
}

func frostfsidRemoveSubjectKey(cmd *cobra.Command, _ []string) {
	addr := getFrostfsIDSubjectAddress(cmd)
	pub := getFrostfsIDSubjectKey(cmd)

	ffsid, err := newFrostfsIDClient(cmd)
	commonCmd.ExitOnErr(cmd, "init contract client: %w", err)

	ffsid.addCall(ffsid.roCli.RemoveSubjectKeyCall(addr, pub))

	err = ffsid.sendWait()
	commonCmd.ExitOnErr(cmd, "remove subject key: %w", err)
}