package task

import (
	"context"

	"github.com/tidbcloud/eks-provider/pkg/deploy"
	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
)

// SyncPhase does not deploy any resources, instead it calculates the
// phase of the network and populate it to the context for the
// subsequent tasks.
type SyncPhase struct {
	*BaseTask
}

func NewSyncPhase(ctx *Context) *SyncPhase {
	return &SyncPhase{BaseTask: &BaseTask{Context: ctx}}
}

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

func (t *SyncPhase) Create(ctx context.Context) deploy.Result {
	if t.Network.Status.Phase != cloudv1.NetworkPhaseNormal {
		t.Network.Status.Phase = cloudv1.NetworkPhaseCreating
	}
	return deploy.FromError(nil)
}

func (t *SyncPhase) Delete(ctx context.Context) deploy.Result {
	if t.Network.Status.Phase != cloudv1.NetworkPhaseDeleting {
		t.Network.Status.Phase = cloudv1.NetworkPhasePendingDelete
	}
	return deploy.FromError(nil)
}

type UpdateStatus struct {
	*BaseTask
}

func NewUpdateStatus(ctx *Context) *UpdateStatus {
	return &UpdateStatus{BaseTask: &BaseTask{Context: ctx}}
}

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

func (t *UpdateStatus) PostCreate(ctx context.Context) deploy.Result {
	return deploy.FromError(t.Local.Status().Update(ctx, t.Network))
}

func (t *UpdateStatus) PostDelete(ctx context.Context) deploy.Result {
	return deploy.FromError(t.Local.Status().Update(ctx, t.Network))
}
