package resources

import (
	"errors"
	"fmt"
	"slices"

	"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/models"
	"cloud.o-forge.io/core/oc-lib/models/live"
	"cloud.o-forge.io/core/oc-lib/models/utils"
	"cloud.o-forge.io/core/oc-lib/tools"
)

type ResourceMongoAccessor[T ResourceInterface] struct {
	utils.AbstractAccessor[ResourceInterface] // AbstractAccessor contains the basic fields of an accessor (model, caller)
}

func sourceFromAccess(access *ResourceAccess) string {
	if access == nil {
		return ""
	}
	if access.Container != nil && access.Container.Source != "" {
		return access.Container.Source
	}
	if access.Source != nil && access.Source.Source != "" {
		return access.Source.Source
	}
	return ""
}

func upsertSourceParam(outputs []models.Param, source string) []models.Param {
	for i, p := range outputs {
		if p.Attr == "source" {
			outputs[i].Value = source
			return outputs
		}
	}
	return append(outputs, models.Param{Attr: "source", Value: source, Readonly: true})
}

func applyAccessSourceOutput(data utils.DBObject) {
	switch r := data.(type) {
	case *ProcessingResource:
		for _, inst := range r.Instances {
			if src := sourceFromAccess(inst.Access); src != "" {
				r.Outputs = upsertSourceParam(r.Outputs, src)
				return
			}
		}
	case *DataResource:
		for _, inst := range r.Instances {
			if src := sourceFromAccess(inst.Access); src != "" {
				r.Outputs = upsertSourceParam(r.Outputs, src)
				return
			}
		}
	}
}

// New creates a new instance of the computeMongoAccessor
func NewAccessor[T ResourceInterface](t tools.DataType, request *tools.APIRequest) *ResourceMongoAccessor[T] {
	if !slices.Contains([]tools.DataType{
		tools.COMPUTE_RESOURCE, tools.STORAGE_RESOURCE,
		tools.PROCESSING_RESOURCE, tools.SERVICE_RESOURCE,
		tools.WORKFLOW_RESOURCE, tools.DATA_RESOURCE, tools.NATIVE_TOOL,
	}, t) {
		return nil
	}
	return &ResourceMongoAccessor[T]{
		AbstractAccessor: utils.AbstractAccessor[ResourceInterface]{
			Logger:  logs.CreateLogger(t.String()), // Create a logger with the data type
			Request: request,
			Type:    t,
			New: func() ResourceInterface {
				switch t {
				case tools.COMPUTE_RESOURCE:
					return &ComputeResource{}
				case tools.STORAGE_RESOURCE:
					return &StorageResource{}
				case tools.PROCESSING_RESOURCE:
					return &ProcessingResource{}
				case tools.SERVICE_RESOURCE:
					return &ServiceResource{}
				case tools.WORKFLOW_RESOURCE:
					return &WorkflowResource{}
				case tools.DATA_RESOURCE:
					return &DataResource{}
				case tools.NATIVE_TOOL:
					return &NativeTool{}
				}
				return nil
			},
		},
	}
}

/*
* Nothing special here, just the basic CRUD operations
 */

func (dca *ResourceMongoAccessor[T]) LoadOne(id string) (utils.DBObject, int, error) {
	data, code, err := dca.AbstractAccessor.LoadOne(id)
	if err == nil {
		data.(T).VerifyBuy()
		data.(T).SetAllowedInstances(dca.Request)
		return data, code, err
	}
	return data, code, err
}

func (dca *ResourceMongoAccessor[T]) UpdateOne(set map[string]interface{}, id string) (utils.DBObject, int, error) {
	if dca.GetType() == tools.COMPUTE_RESOURCE {
		delete(set, "architecture")
		delete(set, "infrastructure")
	} else if dca.GetType() == tools.SERVICE_RESOURCE {
		delete(set, "infrastructure")
	} else if dca.GetType() == tools.STORAGE_RESOURCE {
		delete(set, "storage_type")
	}
	if dca.GetType() == tools.PROCESSING_RESOURCE || dca.GetType() == tools.DATA_RESOURCE {
		if merged, _, _, err := utils.ModelGenericUpdateOne(set, id, dca); err == nil {
			applyAccessSourceOutput(merged)
			if serialized := merged.Serialize(merged); serialized != nil {
				set["outputs"] = serialized["outputs"]
			}
		}
	}
	return utils.GenericUpdateOne(set, id, dca)
}

func (dca *ResourceMongoAccessor[T]) ShouldVerifyAuth() bool {
	return false // TEMP : by pass
}

func (dca *ResourceMongoAccessor[T]) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
	var i string
	idsToUpdate := []string{}
	var a utils.Accessor
	if dca.GetType() == tools.COMPUTE_RESOURCE {
		r := data.(*ComputeResource)
		if len(r.Instances) == 0 {
			return nil, 404, errors.New("can't create a non existing computing units resource with no instances")
		}
		a = live.NewAccessor[*live.LiveDatacenter](tools.LIVE_DATACENTER, &tools.APIRequest{Admin: true})
		res, _, _ := a.LoadOne(r.Instances[0].GetID())
		fmt.Println(res, r.Instances[0].GetID())
		if res == nil {
			return nil, 404, errors.New("can't create a non existing computing units resource not reported onto compute units catalog")
		}
		if !res.(*live.LiveDatacenter).IsCompatible(data.Serialize(data)) {
			return nil, 404, errors.New("live computing units  target is not compatible")
		}
		i = res.GetID()
		idsToUpdate = res.(*live.LiveDatacenter).ResourcesID
	} else if dca.GetType() == tools.SERVICE_RESOURCE {
		r := data.(*ServiceResource)
		if len(r.Instances) == 0 {
			return nil, 404, errors.New("can't create a non existing service resource with no instances")
		}
		a = live.NewAccessor[*live.LiveService](tools.LIVE_SERVICE, &tools.APIRequest{Admin: true})
		res, _, _ := a.LoadOne(r.Instances[0].GetID())
		if res == nil {
			return nil, 404, errors.New("can't create a non existing service resource not reported onto service catalog")
		}
		if !res.(*live.LiveService).IsCompatible(data.Serialize(data)) {
			return nil, 404, errors.New("live service target is not compatible")
		}
		i = res.GetID()
		idsToUpdate = res.(*live.LiveService).ResourcesID
	} else if dca.GetType() == tools.STORAGE_RESOURCE {
		r := data.(*StorageResource)
		if len(r.Instances) == 0 {
			return nil, 404, errors.New("can't create a non existing storage resource with no instances")
		}
		a = live.NewAccessor[*live.LiveStorage](tools.LIVE_STORAGE, &tools.APIRequest{Admin: true})
		res, _, _ := a.LoadOne(r.Instances[0].GetID())
		if res == nil {
			return nil, 404, errors.New("can't create a non existing storage resource not reported onto storage catalog")
		}
		if !res.(*live.LiveStorage).IsCompatible(data.Serialize(data)) {
			return nil, 404, errors.New("live storage target is not compatible")
		}
		i = res.GetID()
		idsToUpdate = res.(*live.LiveStorage).ResourcesID
	}
	applyAccessSourceOutput(data)
	res, code, err := utils.GenericStoreOne(data, dca)
	if res != nil && i != "" {
		idsToUpdate = append(idsToUpdate, res.GetID())
		a.UpdateOne(map[string]interface{}{
			"resources_id": idsToUpdate,
		}, i)
	}
	return res, code, err
}

func (dca *ResourceMongoAccessor[T]) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
	return dca.StoreOne(data)
}

func (wfa *ResourceMongoAccessor[T]) LoadAll(isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) {
	return utils.GenericLoadAll[T](wfa.GetExec(isDraft), isDraft, wfa, offset, limit)
}

func (wfa *ResourceMongoAccessor[T]) Search(filters *dbs.Filters, search string, isDraft bool, offset int64, limit int64) ([]utils.ShallowDBObject, int, error) {
	if filters == nil && search == "*" {
		return utils.GenericLoadAll[T](func(d utils.DBObject) utils.ShallowDBObject {
			fmt.Println("Search", d)
			d.(T).VerifyBuy()
			d.(T).SetAllowedInstances(wfa.Request)
			fmt.Println("Search2", d)
			return d
		}, isDraft, wfa, offset, limit)
	}
	return utils.GenericSearch[T](filters, search, wfa.GetObjectFilters(search),
		func(d utils.DBObject) utils.ShallowDBObject {
			d.(T).VerifyBuy()
			d.(T).SetAllowedInstances(wfa.Request)
			return d
		}, isDraft, wfa, offset, limit)
}

func (a *ResourceMongoAccessor[T]) GetExec(isDraft bool) func(utils.DBObject) utils.ShallowDBObject {
	return func(d utils.DBObject) utils.ShallowDBObject {
		d.(T).VerifyBuy()
		d.(T).SetAllowedInstances(a.Request)
		return d
	}
}

func (abs *ResourceMongoAccessor[T]) GetObjectFilters(search string) *dbs.Filters {
	return &dbs.Filters{
		Or: map[string][]dbs.Filter{ // filter by like name, short_description, description, owner, url if no filters are provided
			"abstractinstanciatedresource.abstractresource.abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
			"abstractinstanciatedresource.abstractresource.type":                {{Operator: dbs.LIKE.String(), Value: search}},
			"abstractinstanciatedresource.abstractresource.short_description":   {{Operator: dbs.LIKE.String(), Value: search}},
			"abstractinstanciatedresource.abstractresource.description":         {{Operator: dbs.LIKE.String(), Value: search}},
			"abstractinstanciatedresource.abstractresource.owners.name":         {{Operator: dbs.LIKE.String(), Value: search}},
		},
	}
}
