package task

import (
	"context"
	"time"

	"github.com/go-logr/logr"
	"github.com/tidbcloud/infra-provider/pkg/deploy"
	"github.com/tidbcloud/infra-provider/pkg/namer"
	"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/config"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/resourcemanager/approute"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/resourcemanager/privatelinkservice"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/resourcemanager/privatelinkservicemigration"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

type Context struct {
	Local                         client.Client
	Logger                        logr.Logger
	BeginAt                       *time.Time
	Namer                         *namer.Namer
	Config                        config.RootConfig
	ClusterCli                    clustercli.ClusterClient
	VPC                           *v1alpha1.VPC
	PrivateLinkService            *v1alpha1.PrivateLinkService
	PrivateLinkServiceRW          privatelinkservice.PrivateLinkServiceReaderWriter
	PrivateLinkServiceMigration   *v1alpha1.PrivateLinkServiceMigration
	PrivateLinkServiceMigrationRW privatelinkservicemigration.PrivateLinkServiceMigrationReaderWriter
	APPRouteR                     approute.APPRouteReader
}

var _ deploy.Task = &BaseTask{}

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

func (c *Context) RoleARN() string {
	if c.VPC.Spec.AccountRef != nil {
		return "arn:aws:iam::" + c.VPC.Spec.AccountRef.AccountID + ":role/" + c.VPC.Spec.AccountRef.Role
	}
	return ""
}

type BaseTask struct {
	*Context
}

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

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

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
}
