package workflow_execution

import (
	"errors"
	"time"

	"cloud.o-forge.io/core/oc-lib/dbs"
	"cloud.o-forge.io/core/oc-lib/logs"
	"cloud.o-forge.io/core/oc-lib/models/common/enum"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
)

type WorkflowExecutionMongoAccessor struct {
	utils.AbstractAccessor[*WorkflowExecution]
	shallow bool
}

func newShallowAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
	return &WorkflowExecutionMongoAccessor{
		shallow: true,
		AbstractAccessor: utils.AbstractAccessor[*WorkflowExecution]{
			Logger:         logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
			Request:        request,
			Type:           tools.WORKFLOW_EXECUTION,
			New:            func() *WorkflowExecution { return &WorkflowExecution{} },
			NotImplemented: []string{"CopyOne"},
		},
	}
}

func NewAccessor(request *tools.APIRequest) *WorkflowExecutionMongoAccessor {
	return &WorkflowExecutionMongoAccessor{
		shallow: false,
		AbstractAccessor: utils.AbstractAccessor[*WorkflowExecution]{
			Logger:         logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
			Request:        request,
			Type:           tools.WORKFLOW_EXECUTION,
			New:            func() *WorkflowExecution { return &WorkflowExecution{} },
			NotImplemented: []string{"CopyOne"},
		},
	}
}

func (wfa *WorkflowExecutionMongoAccessor) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
	if set["state"] == nil {
		return nil, 400, errors.New("state is required")
	}
	return utils.GenericUpdateOne(map[string]interface{}{
		"state": set["state"],
	}, id, wfa)
}

func (a *WorkflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
	return utils.GenericLoadOne(id, a.New(), func(d utils.DBObject) (utils.DBObject, int, error) {
		now := time.Now()
		now = now.Add(time.Second * -60)
		if d.(*WorkflowExecution).State == enum.DRAFT && !a.shallow && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
			utils.GenericDeleteOne(d.GetID(), newShallowAccessor(a.Request))
			return nil, 404, errors.New("not found")
		}
		if d.(*WorkflowExecution).State == enum.SCHEDULED && !a.shallow && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
			d.(*WorkflowExecution).State = enum.FORGOTTEN
			utils.GenericRawUpdateOne(d, id, newShallowAccessor(a.Request))
		}
		return d, 200, nil
	}, a)
}
func (a *WorkflowExecutionMongoAccessor) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
	return func(d utils.DBObject) utils.ShallowDBObject {
		now := time.Now()
		now = now.Add(time.Second * -60)
		if d.(*WorkflowExecution).State == enum.DRAFT && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
			utils.GenericDeleteOne(d.GetID(), newShallowAccessor(a.Request))
			return nil
		}
		if d.(*WorkflowExecution).State == enum.SCHEDULED && now.UTC().After(d.(*WorkflowExecution).ExecDate) {
			d.(*WorkflowExecution).State = enum.FORGOTTEN
			utils.GenericRawUpdateOne(d, d.GetID(), newShallowAccessor(a.Request))
			return d
		}
		return d
	}
}

func (a *WorkflowExecutionMongoAccessor) GetObjectFilters(search string) *dbs.Filters {
	return &dbs.Filters{
		Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
			"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search + "_execution"}},
		}}
}
