package resources

import (
	"encoding/json"
	"errors"
	"fmt"
	"reflect"
	"slices"
	"strings"

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

/*
* DynamicResource is a struct that represents a data resource
* it defines the resource data
 */
type DynamicResource struct {
	AbstractResource
	Type      tools.DataType         `bson:"type,omitempty" json:"type,omitempty"`
	Filters   map[string]interface{} `bson:"filters,omitempty" json:"filters,omitempty"`
	SortRules map[string]string      `bson:"rules,omitempty" json:"rules,omitempty"`

	PeerIds     map[int]string `bson:"peer_ids,omitempty" json:"peer_ids,omitempty"`
	ResourceIds map[int]string `bson:"resource_ids,omitempty" json:"resource_ids,omitempty"`

	SelectedIndex            int  `bson:"selected_index,omitempty" json:"selected_index,omitempty"`
	SelectedPartnershipIndex *int `bson:"selected_partnership_index,omitempty" json:"selected_partnership_index,omitempty"`

	SelectedBuyingStrategy  int `bson:"selected_buying_strategy" json:"selected_buying_strategy,omitempty"`
	SelectedPricingStrategy int `bson:"selected_pricing_strategy" json:"selected_pricing_strategy,omitempty"`

	Instances              []ResourceInstanceITF `bson:"instances,omitempty" json:"instances,omitempty"`
	WatchedDynamicResource []string              `bson:"watched_dynamic_resource,omitempty" json:"watched_dynamic_resource,omitempty"`
}

func (d *DynamicResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
	return nil
}

func (d *DynamicResource) SetAllowedInstances(request *tools.APIRequest, instance_id ...string) []ResourceInstanceITF {
	d.Instances = []ResourceInstanceITF{}
	for k, v := range map[tools.DataType]ResourceInterface{
		tools.COMPUTE_RESOURCE:    &ComputeResource{},
		tools.DATA_RESOURCE:       &DataResource{},
		tools.STORAGE_RESOURCE:    &StorageResource{},
		tools.PROCESSING_RESOURCE: &ProcessingResource{},
		tools.WORKFLOW_RESOURCE:   &WorkflowResource{}} {
		if d.Type != k {
			continue
		}
		access := NewAccessor[*DynamicResource](k, request)
		a, _, _ := access.Search(dbs.FiltersFromFlatMap(d.Filters, v), "", false, 0, 100000)
		fmt.Println(a, dbs.FiltersFromFlatMap(d.Filters, v), d.Filters, v)
		d.PeerIds = map[int]string{}
		d.ResourceIds = map[int]string{}
		for _, res := range a {
			for _, i := range res.(ResourceInterface).SetAllowedInstances(request, instance_id...) {
				d.PeerIds[len(d.Instances)] = res.GetCreatorID()
				d.ResourceIds[len(d.Instances)] = res.GetID()
				d.Instances = append(d.Instances, i)
			}
		}
		break
	}
	sorted := make([]ResourceInstanceITF, len(d.Instances))
	copy(sorted, d.Instances)
	slices.SortStableFunc(sorted, func(a, b ResourceInstanceITF) int {
		d.SortRules["partnerships"] = "%v not contains 2"
		return d.compareByRules(a, b, d.SortRules)
	})
	d.WatchedDynamicResource = []string{}
	return d.Instances
}

func (d *DynamicResource) AddInstances(instance ResourceInstanceITF) {
	d.Instances = append(d.Instances, instance)
}

func (d *DynamicResource) GetSelectedInstance(index *int) ResourceInstanceITF {
	if len(d.Instances) == 0 {
		return nil
	}
	for i, inst := range d.Instances {
		if slices.Contains(d.WatchedDynamicResource, inst.GetID()) {
			continue
		}
		d.WatchedDynamicResource = append(d.WatchedDynamicResource, inst.GetID())
		d.SelectedIndex = i

		for i := range inst.GetPartnerships() {
			if inst.GetProfile(d.PeerIds[i], &i, &d.SelectedBuyingStrategy, &d.SelectedPricingStrategy) != nil {
				d.SelectedPartnershipIndex = &i
				break
			}
		}
		if d.SelectedPartnershipIndex == nil {
			continue
		}
		return inst
	}
	return nil
}

// compareByRules orders instances so those satisfying more sort rules come first.
// When both satisfy a rule, the one with the lower first-attribute value wins (ASC strict).
// Key format: "attrA" for single-%s rules, "attrA,attrB" for two-%s rules.
func (ri *DynamicResource) compareByRules(a, b ResourceInstanceITF, rules map[string]string) int {
	ma := a.Serialize(a)
	mb := b.Serialize(b)
	for attrs, rule := range rules {
		attrPaths := strings.Split(attrs, ",")

		aOk, aFirst := ri.ruleMatchesAny(rule, attrPaths, ma)
		bOk, bFirst := ri.ruleMatchesAny(rule, attrPaths, mb)

		if aOk && !bOk {
			return -1
		}
		if !aOk && bOk {
			return 1
		}
		if aOk && bOk {
			if aFirst < bFirst {
				return -1
			}
			if aFirst > bFirst {
				return 1
			}
		}
	}

	return 0
}

// ruleMatchesAny checks if any value (or combination for 2-%s rules) satisfies rule.
// Arrays at any path level are iterated. Returns (matched, firstMatchingValue).
func (ri *DynamicResource) ruleMatchesAny(rule string, attrPaths []string, m map[string]interface{}) (bool, string) {
	placeholders := strings.Count(rule, "%s")
	if placeholders == 0 {
		return false, ""
	}
	valsA := ri.getVals(strings.Split(strings.TrimSpace(attrPaths[0]), "."), m)
	if placeholders == 1 {
		for _, v := range valsA {
			if ri.byRules(rule, v) {
				return true, fmt.Sprintf("%v", v)
			}
		}
		return false, ""
	}
	if len(attrPaths) < 2 {
		return false, ""
	}
	valsB := ri.getVals(strings.Split(strings.TrimSpace(attrPaths[1]), "."), m)
	for _, a := range valsA {
		for _, b := range valsB {
			if ri.byRules(rule, a, b) {
				return true, fmt.Sprintf("%v", a)
			}
		}
	}
	return false, ""
}

// getVals navigates attrs into m, collecting all leaf values.
// At each level it detects whether the value is a dict (map) or an array and acts accordingly:
// - array of maps  → recurse into each element with the remaining path
// - array of scalars (leaf) → collect all as strings
// - map             → recurse with the remaining path
func (ri *DynamicResource) getVals(attrs []string, m map[string]interface{}) []interface{} {
	if len(attrs) == 0 {
		return nil
	}
	attr := attrs[0]
	if attr == "" || m[attr] == nil {
		return nil
	}
	b, err := json.Marshal(m[attr])
	if err != nil {
		return nil
	}
	// Leaf level: detect array vs scalar.
	if len(attrs) == 1 {
		var arr []interface{}
		if err := json.Unmarshal(b, &arr); err == nil {
			results := []interface{}{}
			for _, v := range arr {
				results = append(results, fmt.Sprintf("%v", v))
			}
			return results
		}
		return []interface{}{m[attr]}
	}
	// Intermediate level: detect array of maps vs single map.
	var arrMaps []map[string]interface{}
	if err := json.Unmarshal(b, &arrMaps); err == nil {
		results := []interface{}{}
		for _, item := range arrMaps {
			results = append(results, ri.getVals(attrs[1:], item)...)
		}
		return results
	}
	nm := map[string]interface{}{}
	if err := json.Unmarshal(b, &nm); err != nil {
		return nil
	}
	return ri.getVals(attrs[1:], nm)
}

func (ri *DynamicResource) byRules(rule string, vals ...interface{}) bool {
	if len(vals) == 0 {
		return false
	}
	formatted := fmt.Sprintf(rule, vals...)
	// hm hm
	switch {
	case strings.Contains(rule, "not contains"):
		a := strings.Split(formatted, " not contains ")
		if reflect.TypeOf(vals[0]).Kind() == reflect.Map {
			return vals[0].(map[string]interface{})[fmt.Sprintf("%v", a[1])] != nil
		}
		return strings.Contains(a[0], a[1])
	case strings.Contains(rule, "contains"):
		a := strings.Split(formatted, " contains ")
		if reflect.TypeOf(vals[0]).Kind() == reflect.Map {
			return vals[0].(map[string]interface{})[fmt.Sprintf("%v", a[1])] != nil
		}
		return strings.Contains(a[0], a[1])
	case strings.Contains(rule, "<="):
		a := strings.Split(formatted, " <= ")
		return len(a) > 1 && a[0] <= a[1]
	case strings.Contains(rule, ">="):
		a := strings.Split(formatted, " >= ")
		return len(a) > 1 && a[0] >= a[1]
	case strings.Contains(rule, "<>"), strings.Contains(rule, "not like"):
		if strings.Contains(rule, "<>") {
			a := strings.Split(formatted, " <> ")
			return len(a) > 1 && !strings.Contains(a[0], a[1]) && !strings.Contains(a[1], a[0])
		}
		a := strings.Split(formatted, " not like ")
		return len(a) > 1 && !strings.Contains(a[0], a[1]) && !strings.Contains(a[1], a[0])
	case strings.Contains(rule, "<"):
		a := strings.Split(formatted, " < ")
		return len(a) > 1 && a[0] < a[1]
	case strings.Contains(rule, ">"):
		a := strings.Split(formatted, " > ")
		return len(a) > 1 && a[0] > a[1]
	case strings.Contains(rule, "=="):
		a := strings.Split(formatted, " == ")
		return len(a) > 1 && a[0] == a[1]
	case strings.Contains(rule, "!="):
		a := strings.Split(formatted, " != ")
		return len(a) > 1 && a[0] != a[1]
	case strings.Contains(rule, "like"):
		a := strings.Split(formatted, " like ")
		return len(a) > 1 && (strings.Contains(a[0], a[1]) || strings.Contains(a[1], a[0]))
	}
	return false
}

func (r *DynamicResource) GetType() string {
	return tools.DYNAMIC_RESOURCE.String()
}

func (abs *DynamicResource) ConvertToPricedResource(t tools.DataType, selectedInstance *int, selectedPartnership *int, selectedBuyingStrategy *int, selectedStrategy *int, selectedBookingModeIndex *int, request *tools.APIRequest) (pricing.PricedItemITF, error) {
	var p pricing.PricedItemITF
	var err error
	for _, v := range []tools.DataType{
		tools.COMPUTE_RESOURCE,
		tools.DATA_RESOURCE,
		tools.STORAGE_RESOURCE,
		tools.PROCESSING_RESOURCE,
		tools.WORKFLOW_RESOURCE,
	} {
		switch v {
		case tools.COMPUTE_RESOURCE:
			if p, err = ConvertToPricedResource[*ComputeResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil {
				return p.(*PricedResource[*ProcessingResourcePricingProfile]), nil
			}
		case tools.DATA_RESOURCE:
			if p, err = ConvertToPricedResource[*DataResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil {
				return p.(*PricedResource[*DataResourcePricingProfile]), nil
			}
		case tools.STORAGE_RESOURCE:
			if p, err = ConvertToPricedResource[*StorageResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil {
				return p.(*PricedResource[*StorageResourcePricingProfile]), nil
			}
		case tools.PROCESSING_RESOURCE:
			if p, err = ConvertToPricedResource[*ProcessingResourcePricingProfile](t, selectedInstance, selectedPartnership, selectedBuyingStrategy, selectedStrategy, selectedBookingModeIndex, abs, request); err == nil {
				return p.(*PricedResource[*ProcessingResourcePricingProfile]), nil
			}
		}
	}
	return nil, errors.New("can't convert priced resource")
}
