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
	shallow bool
}

func newShallowAccessor(request *tools.APIRequest) *workflowExecutionMongoAccessor {
	return &workflowExecutionMongoAccessor{
		shallow: true,
		AbstractAccessor: utils.AbstractAccessor{
			Logger:  logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
			Request: request,
			Type:    tools.WORKFLOW_EXECUTION,
		},
	}
}

func NewAccessor(request *tools.APIRequest) *workflowExecutionMongoAccessor {
	return &workflowExecutionMongoAccessor{
		shallow: false,
		AbstractAccessor: utils.AbstractAccessor{
			Logger:  logs.CreateLogger(tools.WORKFLOW_EXECUTION.String()), // Create a logger with the data type
			Request: request,
			Type:    tools.WORKFLOW_EXECUTION,
		},
	}
}

func (wfa *workflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
	return nil, 404, errors.New("not implemented")
}

func (wfa *workflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
	if set.(*WorkflowExecutions).State == 0 {
		return nil, 400, errors.New("state is required")
	}
	realSet := WorkflowExecutions{State: set.(*WorkflowExecutions).State}
	return utils.GenericUpdateOne(&realSet, id, wfa, &WorkflowExecutions{})
}

func (wfa *workflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
	return nil, 404, errors.New("not implemented")
}

func (wfa *workflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
	return nil, 404, errors.New("not implemented")
}

func (a *workflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
	return utils.GenericLoadOne[*WorkflowExecutions](id, func(d utils.DBObject) (utils.DBObject, int, error) {
		if d.(*WorkflowExecutions).State == enum.DRAFT && !a.shallow && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
			utils.GenericDeleteOne(d.GetID(), newShallowAccessor(a.Request))
			return nil, 404, errors.New("not found")
		}
		if d.(*WorkflowExecutions).State == enum.SCHEDULED && !a.shallow && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
			d.(*WorkflowExecutions).State = enum.FORGOTTEN
			utils.GenericRawUpdateOne(d, id, newShallowAccessor(a.Request))
		}
		return d, 200, nil
	}, a)
}

func (a *workflowExecutionMongoAccessor) LoadAll(isDraft bool) ([]utils.ShallowDBObject, int, error) {
	return utils.GenericLoadAll[*WorkflowExecutions](a.getExec(), isDraft, a)
}

func (a *workflowExecutionMongoAccessor) Search(filters *dbs.Filters, search string, isDraft bool) ([]utils.ShallowDBObject, int, error) {
	return utils.GenericSearch[*WorkflowExecutions](filters, search, a.GetExecFilters(search), a.getExec(), isDraft, a)
}

func (a *workflowExecutionMongoAccessor) getExec() func(utils.DBObject) utils.ShallowDBObject {
	return func(d utils.DBObject) utils.ShallowDBObject {
		if d.(*WorkflowExecutions).State == enum.DRAFT && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
			utils.GenericDeleteOne(d.GetID(), newShallowAccessor(a.Request))
			return nil
		}
		if d.(*WorkflowExecutions).State == enum.SCHEDULED && time.Now().UTC().After(d.(*WorkflowExecutions).ExecDate) {
			d.(*WorkflowExecutions).State = enum.FORGOTTEN
			utils.GenericRawUpdateOne(d, d.GetID(), newShallowAccessor(a.Request))
			return d
		}
		return d
	}
}

func (a *workflowExecutionMongoAccessor) GetExecFilters(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"}},
		}}
}
