package vpc_endpoint

import (
	"context"

	"github.com/pingcap/errors"
	"sigs.k8s.io/controller-runtime/pkg/client"

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

func DeployGApiPscEndpoint(ctx context.Context, infraCli client.Client, gcpapi gcp.GCPAPI, network *cloudv1.Network, currentVersion int, projectID, networkName, shootName string) error {
	if !ShouldUpdateEndpoint(network.Annotations, currentVersion) {
		return nil
	}

	if err := gcpapi.CreateGoogleApiPsc(ctx, projectID, network.Spec.Region, networkName, shootName); err != nil {
		return errors.Annotate(err, "failed to create psc for google api")
	}
	network.Annotations = AddVersionToAnnotations(network.Annotations, currentVersion)
	if err := infraCli.Update(ctx, network); err != nil {
		return errors.Annotate(err, "failed to update network status")
	}
	return nil
}
