package task

import (
	"context"
	"time"

	"github.com/go-logr/logr"
	"github.com/tidbcloud/infra-provider/pkg/deploy"
	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/common/clustercli"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/common/gcpcontainer"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/config"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/resourcemanager/approute"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/resourcemanager/vpc"
	agentcontrolapi "github.com/tidbcloud/observability/agent-control-server/pkg/agentcontrol/api"
	"k8s.io/apimachinery/pkg/runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
	Finalizer = "approute.dedicated.regionserver.tidbcloud.com/finalizer"
)

type Context struct {
	Scheme          *runtime.Scheme
	Local           client.Client
	Logger          logr.Logger
	APPBox          *v1alpha1.APPBox
	APPRoute        *v1alpha1.APPRoute
	APPRouteRW      approute.APPRouteReaderWriter
	VPCR            vpc.VPCReader
	Config          config.RootConfig // config is useless in this context
	ClusterCli      clustercli.ClusterClient
	GCPContainerCli gcpcontainer.ClusterGetter // optional, only used for gcp
	AgentControlAPI agentcontrolapi.AgentControlAPI
	BeginAt         *time.Time
}

var _ deploy.Task = &BaseTask{}

func (c *Context) Default() (*Context, error) {
	return c, nil
}

type BaseTask struct {
	*Context
}

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

func (t *BaseTask) Category() deploy.TaskCategory {
	return deploy.NetworkTask
}

func (t *BaseTask) Create(ctx context.Context) deploy.Result {
	return deploy.Done()
}

func (t *BaseTask) PostCreate(ctx context.Context) deploy.Result {
	return deploy.Done()
}

func (t *BaseTask) Delete(ctx context.Context) deploy.Result {
	return deploy.Done()
}

func (t *BaseTask) PostDelete(ctx context.Context) deploy.Result {
	return deploy.Done()
}

func (t *BaseTask) MetricsStartTime() *time.Time {
	return t.BeginAt
}
