package control

import (
	"fmt"
	"strings"

	"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
	commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
	apeCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common/ape"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
	apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
	policyengine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
	"github.com/spf13/cobra"
)

var listRulesCmd = &cobra.Command{
	Use:   "list-rules",
	Short: "List local overrides",
	Long:  "List local APE overrides of the node",
	Run:   listRules,
}

var engineToControlSvcType = map[policyengine.TargetType]control.ChainTarget_TargetType{
	policyengine.Namespace: control.ChainTarget_NAMESPACE,
	policyengine.Container: control.ChainTarget_CONTAINER,
	policyengine.User:      control.ChainTarget_USER,
	policyengine.Group:     control.ChainTarget_GROUP,
}

func parseTarget(cmd *cobra.Command) *control.ChainTarget {
	target := apeCmd.ParseTarget(cmd)

	typ, ok := engineToControlSvcType[target.Type]
	if !ok {
		commonCmd.ExitOnErr(cmd, "%w", fmt.Errorf("unknown type '%c", target.Type))
	}

	return &control.ChainTarget{
		Name: target.Name,
		Type: typ,
	}
}

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

	target := parseTarget(cmd)
	req := &control.ListChainLocalOverridesRequest{
		Body: &control.ListChainLocalOverridesRequest_Body{
			Target: target,
		},
	}

	signRequest(cmd, pk, req)

	cli := getClient(cmd, pk)

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

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

	chains := resp.GetBody().GetChains()
	if len(chains) == 0 {
		cmd.Printf("Local overrides are not defined for the %s.\n", strings.ToLower(target.GetType().String()))
		return
	}

	for _, c := range chains {
		var chain apechain.Chain
		commonCmd.ExitOnErr(cmd, "decode error: %w", chain.DecodeBytes(c))
		apeCmd.PrintHumanReadableAPEChain(cmd, &chain)
	}
}

func initControlListRulesCmd() {
	initControlFlags(listRulesCmd)

	ff := listRulesCmd.Flags()
	ff.String(apeCmd.TargetNameFlag, "", apeCmd.TargetNameFlagDesc)
	ff.String(apeCmd.TargetTypeFlag, "", apeCmd.TargetTypeFlagDesc)
	_ = listRulesCmd.MarkFlagRequired(apeCmd.TargetTypeFlag)
}