package control

import (
	rawclient "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"
	ircontrol "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir"
	ircontrolsrv "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir/server"
	"github.com/spf13/cobra"
)

var irHealthCheckCmd = &cobra.Command{
	Use:   "healthcheck",
	Short: "Health check for FrostFS inner ring nodes",
	Long:  "Health check for FrostFS inner ring nodes.",
	Run:   irHealthCheck,
}

func initControlIRHealthCheckCmd() {
	initControlFlags(irHealthCheckCmd)
}

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

	req := new(ircontrol.HealthCheckRequest)

	req.SetBody(new(ircontrol.HealthCheckRequest_Body))

	err := ircontrolsrv.SignMessage(pk, req)
	commonCmd.ExitOnErr(cmd, "could not sign request: %w", err)

	var resp *ircontrol.HealthCheckResponse
	err = cli.ExecRaw(func(client *rawclient.Client) error {
		resp, err = ircontrol.HealthCheck(client, req)
		return err
	})
	commonCmd.ExitOnErr(cmd, "rpc error: %w", err)

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

	cmd.Printf("Health status: %s\n", resp.GetBody().GetHealthStatus())
}