package common

import (
	"context"
	"fmt"
	"time"

	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/controllers/appbox/task"

	"github.com/tidbcloud/infra-provider/pkg/deploy"
)

// Guardian guards the deletion and creation of the network.
type Guardian struct {
	*task.BaseTask
}

func NewGuardian(ctx *task.Context) *Guardian {
	return &Guardian{BaseTask: &task.BaseTask{Context: ctx}}
}

func (t *Guardian) Name() string {
	return "Guardian"
}

func (t *Guardian) Create(ctx context.Context) deploy.Result {
	// if the APPBox is not active, we should wait for it to be active
	if t.APPBox.Status.Phase != v1alpha1.APPBoxPhaseActive {
		return deploy.Panic(fmt.Errorf("APPBox is not active")).WithRequeuAfter(30 * time.Second)
	}
	return deploy.Done()
}
