package infraapi

import (
	"context"
	"strconv"

	"github.com/pingcap/errors"
	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

// ListBackups lists all backup CRs for the cluster.
func (a *API) ListBackups(ctx context.Context, projectID, clusterID uint64) ([]cloudv1.Backup, error) {
	var (
		ns           = Namespace(a.projectName, projectID)
		clusterIDStr = strconv.FormatUint(clusterID, 10)
		backupList   cloudv1.BackupList
	)
	err := a.client.List(ctx, &backupList, client.InNamespace(ns), client.MatchingLabels{clusterLabel: clusterIDStr})
	if err != nil {
		return nil, errors.Annotatef(err, "list backup CRs for cluster %q in namespace %q failed", clusterIDStr, ns)
	}
	return backupList.Items, nil
}
