package task

import (
	"context"
	"time"

	"github.com/go-logr/logr"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	o11yv1 "github.com/tidbcloud/observability/api/regionalserver/resourceworker/v1"
	"sigs.k8s.io/controller-runtime/pkg/client"

	"github.com/tidbcloud/observability/pkg/common/deploy"
	"github.com/tidbcloud/observability/pkg/common/structs"
)

const (
	Finalizer = "observability.pingcap.com/integrationtask"
)

type Context struct {
	Task     *o11yv1.IntegrationTask
	original *o11yv1.IntegrationTask
	// Injected from the caller
	Logger      logr.Logger
	Client      client.Client
	ServiceInfo structs.ServiceInfo
	// BeginAt denotes the starting time of the task.
	BeginAt *time.Time
}

func (c *Context) Default() (*Context, error) {
	c.original = c.Task.DeepCopy()
	return c, nil
}

func (c *Context) IsTaskChanged() bool {
	diff := cmp.Diff(c.original, c.Task, cmpopts.EquateEmpty())
	if diff == "" {
		c.Logger.Info("integration task is synced")
		return false
	}
	c.Logger.Info("integration task is not synced", "diff", diff)
	return true
}

var _ deploy.Task = &BaseTask{}

type BaseTask struct {
	*Context
}

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

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

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()
}

// MetricsStartTime will return the `BeginAt` (nil by default). For
// Task whose metrics need to be exported, the `BeginAt` should be
// initialized when the `Create` is being called.
func (t *BaseTask) MetricsStartTime() *time.Time {
	return t.Context.BeginAt
}
