package wait

import (
	"context"
	"fmt"
	"time"

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

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

func WaitForNetworkPhase(
	ctx context.Context,
	c client.Client,
	net *cloudv1.Network,
	phase string,
	timeout time.Duration,
) error {
	cond := func() error {
		if net.Status.Phase == phase {
			return nil
		}
		return fmt.Errorf("expected phase is %s, actual is %v", phase, net.Status.Phase)
	}

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

	return nil
}
