package wait

import (
	"context"
	"fmt"
	"time"

	"sigs.k8s.io/controller-runtime/pkg/client"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
)

func WaitForClusterPhase(
	ctx context.Context,
	c client.Client,
	cluster *cloudv1.Cluster,
	phase cloudv1.ClusterPhase,
	timeout time.Duration,
) error {
	cond := func() bool {
		return cluster.Status.Phase == phase
	}

	if err := WaitForCondition(ctx, c, cluster, cond, timeout); err != nil {
		return fmt.Errorf("cluster phase is not %s: %w", phase, err)
	}

	return nil
}
