package wait

import (
	"context"
	"fmt"
	"time"

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

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

func WaitForRestorePhase(
	ctx context.Context,
	c client.Client,
	restore *cloudv1.Restore,
	phase cloudv1.RestoreConditionType,
	timeout time.Duration,
) error {
	cond := func() bool {
		return restore.Status.Phase == phase
	}

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

	return nil
}
