package kafka

import (
	"github.com/confluentinc/kafka-rest-sdk-go/kafkarestv3"
	"github.com/spf13/cobra"

	pcmd "github.com/confluentinc/cli/internal/pkg/cmd"
	"github.com/confluentinc/cli/internal/pkg/output"
)

func newLinkOnPrem(data kafkarestv3.ListLinksResponseData, topic string) *link {
	l := &link{
		LinkName:  data.LinkName,
		TopicName: topic,
	}

	if data.SourceClusterId != nil {
		l.SourceClusterId = *data.SourceClusterId
	}

	if data.DestinationClusterId != nil {
		l.DestinationClusterId = *data.DestinationClusterId
	}

	return l
}

func (c *linkCommand) newListCommandOnPrem() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "list",
		Short: "List previously created cluster links.",
		Long:  "List previously created cluster links if the provided cluster is a destination cluster.",
		Args:  cobra.NoArgs,
		RunE:  c.listOnPrem,
	}

	cmd.Flags().Bool(includeTopicsFlagName, false, "If set, will list mirrored topics for the links returned.")
	cmd.Flags().AddFlagSet(pcmd.OnPremKafkaRestSet())
	pcmd.AddContextFlag(cmd, c.CLICommand)
	pcmd.AddOutputFlag(cmd)

	return cmd
}

func (c *linkCommand) listOnPrem(cmd *cobra.Command, _ []string) error {
	includeTopics, err := cmd.Flags().GetBool(includeTopicsFlagName)
	if err != nil {
		return err
	}

	client, ctx, err := initKafkaRest(c.AuthenticatedCLICommand, cmd)
	if err != nil {
		return err
	}

	clusterId, err := getClusterIdForRestRequests(client, ctx)
	if err != nil {
		return err
	}

	listLinksRespDataList, httpResp, err := client.ClusterLinkingV3Api.ListKafkaLinks(ctx, clusterId)
	if err != nil {
		return handleOpenApiError(httpResp, err, client)
	}

	listFields := getListFieldsOnPrem(includeTopics)
	humanLabels := camelToSpaced(listFields)
	structuredLabels := camelToSnake(listFields)

	w, err := output.NewListOutputWriter(cmd, listFields, humanLabels, structuredLabels)
	if err != nil {
		return err
	}

	for _, data := range listLinksRespDataList.Data {
		if includeTopics && len(data.TopicsNames) > 0 {
			for _, topic := range data.TopicsNames {
				w.AddElement(newLinkOnPrem(data, topic))
			}
		} else {
			w.AddElement(newLinkOnPrem(data, ""))
		}
	}

	return w.Out()
}
