package control

import (
	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
	"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
	commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
	"github.com/spf13/cobra"
)

const (
	concurrencyFlag      = "concurrency"
	removeDuplicatesFlag = "remove-duplicates"
)

var doctorCmd = &cobra.Command{
	Use:   "doctor",
	Short: "Restructure node's storage",
	Long:  "Restructure node's storage",
	Run:   doctor,
}

func doctor(cmd *cobra.Command, _ []string) {
	pk := key.Get(cmd)

	req := &control.DoctorRequest{Body: new(control.DoctorRequest_Body)}
	req.Body.Concurrency, _ = cmd.Flags().GetUint32(concurrencyFlag)
	req.Body.RemoveDuplicates, _ = cmd.Flags().GetBool(removeDuplicatesFlag)

	signRequest(cmd, pk, req)

	cli := getClient(cmd, pk)

	var resp *control.DoctorResponse
	var err error
	err = cli.ExecRaw(func(client *client.Client) error {
		resp, err = control.Doctor(client, req)
		return err
	})
	commonCmd.ExitOnErr(cmd, "rpc error: %w", err)

	verifyResponse(cmd, resp.GetSignature(), resp.GetBody())

	cmd.Println("Operation has finished.")
}

func initControlDoctorCmd() {
	initControlFlags(doctorCmd)

	ff := doctorCmd.Flags()
	ff.Uint32(concurrencyFlag, 0, "Number of parallel threads to use")
	ff.Bool(removeDuplicatesFlag, false, "Remove duplicate objects")
}