/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>
*/

package config

import (
	"slices"
	"time"

	"github.com/cgrates/birpc/context"
	"github.com/cgrates/cgrates/utils"
)

const (
	ResourcesUsageIDDftOpt  = utils.EmptyString
	ResourcesUsageTTLDftOpt = 72 * time.Hour
	ResourcesUnitsDftOpt    = 1
)

type ResourcesOpts struct {
	UsageID  []*DynamicStringOpt
	UsageTTL []*DynamicDurationOpt
	Units    []*DynamicFloat64Opt
}

// ResourceSConfig is resorces section config
type ResourceSConfig struct {
	Enabled                bool
	IndexedSelects         bool
	ThresholdSConns        []string
	StoreInterval          time.Duration // Dump regularly from cache into dataDB
	StringIndexedFields    *[]string
	PrefixIndexedFields    *[]string
	SuffixIndexedFields    *[]string
	ExistsIndexedFields    *[]string
	NotExistsIndexedFields *[]string
	NestedFields           bool
	Opts                   *ResourcesOpts
}

// loadResourceSCfg loads the ResourceS section of the configuration
func (rlcfg *ResourceSConfig) Load(ctx *context.Context, jsnCfg ConfigDB, _ *CGRConfig) (err error) {
	jsnRLSCfg := new(ResourceSJsonCfg)
	if err = jsnCfg.GetSection(ctx, ResourceSJSON, jsnRLSCfg); err != nil {
		return
	}
	return rlcfg.loadFromJSONCfg(jsnRLSCfg)
}

func (rsOpts *ResourcesOpts) loadFromJSONCfg(jsnCfg *ResourcesOptsJson) (err error) {
	if jsnCfg == nil {
		return
	}
	if jsnCfg.UsageID != nil {
		var usageID []*DynamicStringOpt
		usageID, err = InterfaceToDynamicStringOpts(jsnCfg.UsageID)
		rsOpts.UsageID = append(usageID, rsOpts.UsageID...)
	}
	if jsnCfg.UsageTTL != nil {
		var usageTTL []*DynamicDurationOpt
		if usageTTL, err = IfaceToDurationDynamicOpts(jsnCfg.UsageTTL); err != nil {
			return
		}
		rsOpts.UsageTTL = append(usageTTL, rsOpts.UsageTTL...)
	}

	if jsnCfg.Units != nil {
		var units []*DynamicFloat64Opt
		units, err = InterfaceToFloat64DynamicOpts(jsnCfg.Units)
		rsOpts.Units = append(units, rsOpts.Units...)
	}
	return
}

func (rlcfg *ResourceSConfig) loadFromJSONCfg(jsnCfg *ResourceSJsonCfg) (err error) {
	if jsnCfg == nil {
		return
	}
	if jsnCfg.Enabled != nil {
		rlcfg.Enabled = *jsnCfg.Enabled
	}
	if jsnCfg.Indexed_selects != nil {
		rlcfg.IndexedSelects = *jsnCfg.Indexed_selects
	}
	if jsnCfg.Thresholds_conns != nil {
		rlcfg.ThresholdSConns = tagInternalConns(*jsnCfg.Thresholds_conns, utils.MetaThresholds)
	}
	if jsnCfg.Store_interval != nil {
		if rlcfg.StoreInterval, err = utils.ParseDurationWithNanosecs(*jsnCfg.Store_interval); err != nil {
			return
		}
	}
	if jsnCfg.String_indexed_fields != nil {
		rlcfg.StringIndexedFields = utils.SliceStringPointer(slices.Clone(*jsnCfg.String_indexed_fields))
	}
	if jsnCfg.Prefix_indexed_fields != nil {
		rlcfg.PrefixIndexedFields = utils.SliceStringPointer(slices.Clone(*jsnCfg.Prefix_indexed_fields))
	}
	if jsnCfg.Suffix_indexed_fields != nil {
		rlcfg.SuffixIndexedFields = utils.SliceStringPointer(slices.Clone(*jsnCfg.Suffix_indexed_fields))
	}
	if jsnCfg.Exists_indexed_fields != nil {
		rlcfg.ExistsIndexedFields = utils.SliceStringPointer(slices.Clone(*jsnCfg.Exists_indexed_fields))
	}
	if jsnCfg.Notexists_indexed_fields != nil {
		rlcfg.NotExistsIndexedFields = utils.SliceStringPointer(slices.Clone(*jsnCfg.Notexists_indexed_fields))
	}
	if jsnCfg.Nested_fields != nil {
		rlcfg.NestedFields = *jsnCfg.Nested_fields
	}
	if jsnCfg.Opts != nil {
		err = rlcfg.Opts.loadFromJSONCfg(jsnCfg.Opts)
	}
	return
}

// AsMapInterface returns the config as a map[string]any
func (rlcfg ResourceSConfig) AsMapInterface() any {
	opts := map[string]any{
		utils.MetaUsageIDCfg:  rlcfg.Opts.UsageID,
		utils.MetaUsageTTLCfg: rlcfg.Opts.UsageTTL,
		utils.MetaUnitsCfg:    rlcfg.Opts.Units,
	}
	mp := map[string]any{
		utils.EnabledCfg:        rlcfg.Enabled,
		utils.IndexedSelectsCfg: rlcfg.IndexedSelects,
		utils.NestedFieldsCfg:   rlcfg.NestedFields,
		utils.StoreIntervalCfg:  utils.EmptyString,
		utils.OptsCfg:           opts,
	}
	if rlcfg.ThresholdSConns != nil {
		mp[utils.ThresholdSConnsCfg] = stripInternalConns(rlcfg.ThresholdSConns)
	}
	if rlcfg.StringIndexedFields != nil {
		mp[utils.StringIndexedFieldsCfg] = slices.Clone(*rlcfg.StringIndexedFields)
	}
	if rlcfg.PrefixIndexedFields != nil {
		mp[utils.PrefixIndexedFieldsCfg] = slices.Clone(*rlcfg.PrefixIndexedFields)
	}
	if rlcfg.SuffixIndexedFields != nil {
		mp[utils.SuffixIndexedFieldsCfg] = slices.Clone(*rlcfg.SuffixIndexedFields)
	}
	if rlcfg.ExistsIndexedFields != nil {
		mp[utils.ExistsIndexedFieldsCfg] = slices.Clone(*rlcfg.ExistsIndexedFields)
	}
	if rlcfg.NotExistsIndexedFields != nil {
		mp[utils.NotExistsIndexedFieldsCfg] = slices.Clone(*rlcfg.NotExistsIndexedFields)
	}
	if rlcfg.StoreInterval != 0 {
		mp[utils.StoreIntervalCfg] = rlcfg.StoreInterval.String()
	}
	return mp
}

func (ResourceSConfig) SName() string               { return ResourceSJSON }
func (rlcfg ResourceSConfig) CloneSection() Section { return rlcfg.Clone() }

func (rsOpts *ResourcesOpts) Clone() (cln *ResourcesOpts) {
	var usageID []*DynamicStringOpt
	if rsOpts.UsageID != nil {
		usageID = CloneDynamicStringOpt(rsOpts.UsageID)
	}
	var usageTTL []*DynamicDurationOpt
	if rsOpts.UsageTTL != nil {
		usageTTL = CloneDynamicDurationOpt(rsOpts.UsageTTL)
	}
	var units []*DynamicFloat64Opt
	if rsOpts.Units != nil {
		units = CloneDynamicFloat64Opt(rsOpts.Units)
	}
	cln = &ResourcesOpts{
		UsageID:  usageID,
		UsageTTL: usageTTL,
		Units:    units,
	}
	return
}

// Clone returns a deep copy of ResourceSConfig
func (rlcfg ResourceSConfig) Clone() (cln *ResourceSConfig) {
	cln = &ResourceSConfig{
		Enabled:        rlcfg.Enabled,
		IndexedSelects: rlcfg.IndexedSelects,
		StoreInterval:  rlcfg.StoreInterval,
		NestedFields:   rlcfg.NestedFields,
		Opts:           rlcfg.Opts.Clone(),
	}
	if rlcfg.ThresholdSConns != nil {
		cln.ThresholdSConns = slices.Clone(rlcfg.ThresholdSConns)
	}

	if rlcfg.StringIndexedFields != nil {
		cln.StringIndexedFields = utils.SliceStringPointer(slices.Clone(*rlcfg.StringIndexedFields))
	}
	if rlcfg.PrefixIndexedFields != nil {
		cln.PrefixIndexedFields = utils.SliceStringPointer(slices.Clone(*rlcfg.PrefixIndexedFields))
	}
	if rlcfg.SuffixIndexedFields != nil {
		cln.SuffixIndexedFields = utils.SliceStringPointer(slices.Clone(*rlcfg.SuffixIndexedFields))
	}
	if rlcfg.ExistsIndexedFields != nil {
		cln.ExistsIndexedFields = utils.SliceStringPointer(slices.Clone(*rlcfg.ExistsIndexedFields))
	}
	if rlcfg.NotExistsIndexedFields != nil {
		cln.NotExistsIndexedFields = utils.SliceStringPointer(slices.Clone(*rlcfg.NotExistsIndexedFields))
	}
	return
}

type ResourcesOptsJson struct {
	UsageID  []*DynamicInterfaceOpt `json:"*usageID"`
	UsageTTL []*DynamicInterfaceOpt `json:"*usageTTL"`
	Units    []*DynamicInterfaceOpt `json:"*units"`
}

// ResourceLimiter service config section
type ResourceSJsonCfg struct {
	Enabled                  *bool
	Indexed_selects          *bool
	Thresholds_conns         *[]string
	Store_interval           *string
	String_indexed_fields    *[]string
	Prefix_indexed_fields    *[]string
	Suffix_indexed_fields    *[]string
	Exists_indexed_fields    *[]string
	Notexists_indexed_fields *[]string
	Nested_fields            *bool // applies when indexed fields is not defined
	Opts                     *ResourcesOptsJson
}

func diffResourcesOptsJsonCfg(d *ResourcesOptsJson, v1, v2 *ResourcesOpts) *ResourcesOptsJson {
	if d == nil {
		d = new(ResourcesOptsJson)
	}
	if !DynamicStringOptEqual(v1.UsageID, v2.UsageID) {
		d.UsageID = DynamicStringToInterfaceOpts(v2.UsageID)
	}
	if !DynamicDurationOptEqual(v1.UsageTTL, v2.UsageTTL) {
		d.UsageTTL = DurationToIfaceDynamicOpts(v2.UsageTTL)
	}
	if !DynamicFloat64OptEqual(v1.Units, v2.Units) {
		d.Units = Float64ToInterfaceDynamicOpts(v2.Units)
	}
	return d
}

func diffResourceSJsonCfg(d *ResourceSJsonCfg, v1, v2 *ResourceSConfig) *ResourceSJsonCfg {
	if d == nil {
		d = new(ResourceSJsonCfg)
	}
	if v1.Enabled != v2.Enabled {
		d.Enabled = utils.BoolPointer(v2.Enabled)
	}
	if v1.IndexedSelects != v2.IndexedSelects {
		d.Indexed_selects = utils.BoolPointer(v2.IndexedSelects)
	}
	if !slices.Equal(v1.ThresholdSConns, v2.ThresholdSConns) {
		d.Thresholds_conns = utils.SliceStringPointer(stripInternalConns(v2.ThresholdSConns))
	}
	if v1.StoreInterval != v2.StoreInterval {
		d.Store_interval = utils.StringPointer(v2.StoreInterval.String())
	}
	d.String_indexed_fields = diffIndexSlice(d.String_indexed_fields, v1.StringIndexedFields, v2.StringIndexedFields)
	d.Prefix_indexed_fields = diffIndexSlice(d.Prefix_indexed_fields, v1.PrefixIndexedFields, v2.PrefixIndexedFields)
	d.Suffix_indexed_fields = diffIndexSlice(d.Suffix_indexed_fields, v1.SuffixIndexedFields, v2.SuffixIndexedFields)
	d.Exists_indexed_fields = diffIndexSlice(d.Exists_indexed_fields, v1.ExistsIndexedFields, v2.ExistsIndexedFields)
	d.Notexists_indexed_fields = diffIndexSlice(d.Notexists_indexed_fields, v1.NotExistsIndexedFields, v2.NotExistsIndexedFields)
	if v1.NestedFields != v2.NestedFields {
		d.Nested_fields = utils.BoolPointer(v2.NestedFields)
	}
	d.Opts = diffResourcesOptsJsonCfg(d.Opts, v1.Opts, v2.Opts)
	return d
}
