/*
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"

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

// AdminSCfg is the configuration of Apier service
type AdminSCfg struct {
	Enabled         bool
	CachesConns     []string // connections towards Cache
	ActionSConns    []string // connections towards Scheduler
	AttributeSConns []string // connections towards AttributeS
	EEsConns        []string // connections towards EEs
}

// loadApierCfg loads the Apier section of the configuration
func (aCfg *AdminSCfg) Load(ctx *context.Context, jsnCfg ConfigDB, _ *CGRConfig) (err error) {
	jsnApierCfg := new(AdminSJsonCfg)
	if err = jsnCfg.GetSection(ctx, AdminSJSON, jsnApierCfg); err != nil {
		return
	}
	return aCfg.loadFromJSONCfg(jsnApierCfg)
}

func (aCfg *AdminSCfg) loadFromJSONCfg(jsnCfg *AdminSJsonCfg) (err error) {
	if jsnCfg == nil {
		return
	}
	if jsnCfg.Enabled != nil {
		aCfg.Enabled = *jsnCfg.Enabled
	}
	if jsnCfg.Caches_conns != nil {
		aCfg.CachesConns = tagInternalConns(*jsnCfg.Caches_conns, utils.MetaCaches)
	}
	if jsnCfg.Actions_conns != nil {
		aCfg.ActionSConns = tagInternalConns(*jsnCfg.Actions_conns, utils.MetaActions)
	}
	if jsnCfg.Attributes_conns != nil {
		aCfg.AttributeSConns = tagInternalConns(*jsnCfg.Attributes_conns, utils.MetaAttributes)
	}
	if jsnCfg.Ees_conns != nil {
		aCfg.EEsConns = tagInternalConns(*jsnCfg.Ees_conns, utils.MetaEEs)
	}
	return
}

// AsMapInterface returns the config as a map[string]any
func (aCfg AdminSCfg) AsMapInterface() any {
	mp := map[string]any{
		utils.EnabledCfg: aCfg.Enabled,
	}
	if aCfg.CachesConns != nil {
		mp[utils.CachesConnsCfg] = stripInternalConns(aCfg.CachesConns)
	}
	if aCfg.ActionSConns != nil {
		mp[utils.ActionSConnsCfg] = stripInternalConns(aCfg.ActionSConns)
	}
	if aCfg.AttributeSConns != nil {
		mp[utils.AttributeSConnsCfg] = stripInternalConns(aCfg.AttributeSConns)
	}
	if aCfg.EEsConns != nil {
		mp[utils.EEsConnsCfg] = stripInternalConns(aCfg.EEsConns)
	}
	return mp
}

func (AdminSCfg) SName() string              { return AdminSJSON }
func (aCfg AdminSCfg) CloneSection() Section { return aCfg.Clone() }

// Clone returns a deep copy of ApierCfg
func (aCfg AdminSCfg) Clone() (cln *AdminSCfg) {
	cln = &AdminSCfg{
		Enabled: aCfg.Enabled,
	}
	if aCfg.CachesConns != nil {
		cln.CachesConns = slices.Clone(aCfg.CachesConns)
	}
	if aCfg.ActionSConns != nil {
		cln.ActionSConns = slices.Clone(aCfg.ActionSConns)
	}
	if aCfg.AttributeSConns != nil {
		cln.AttributeSConns = slices.Clone(aCfg.AttributeSConns)
	}
	if aCfg.EEsConns != nil {
		cln.EEsConns = slices.Clone(aCfg.EEsConns)
	}
	return
}

type AdminSJsonCfg struct {
	Enabled          *bool
	Caches_conns     *[]string
	Actions_conns    *[]string
	Attributes_conns *[]string
	Ees_conns        *[]string
}

func diffAdminSJsonCfg(d *AdminSJsonCfg, v1, v2 *AdminSCfg) *AdminSJsonCfg {
	if d == nil {
		d = new(AdminSJsonCfg)
	}
	if v1.Enabled != v2.Enabled {
		d.Enabled = utils.BoolPointer(v2.Enabled)
	}
	if !slices.Equal(v1.CachesConns, v2.CachesConns) {
		d.Caches_conns = utils.SliceStringPointer(stripInternalConns(v2.CachesConns))
	}
	if !slices.Equal(v1.ActionSConns, v2.ActionSConns) {
		d.Actions_conns = utils.SliceStringPointer(stripInternalConns(v2.ActionSConns))
	}
	if !slices.Equal(v1.AttributeSConns, v2.AttributeSConns) {
		d.Attributes_conns = utils.SliceStringPointer(stripInternalConns(v2.AttributeSConns))
	}
	if !slices.Equal(v1.EEsConns, v2.EEsConns) {
		d.Ees_conns = utils.SliceStringPointer(stripInternalConns(v2.EEsConns))
	}
	return d
}
