package wait

import (
	"context"
	"fmt"
	"time"

	infra "github.com/tidbcloud/tidb-management-service/api/serverless_infra"
	infraclient "github.com/tidbcloud/tidb-management-service/api/serverless_infra/client"
)

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

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

	return nil
}
