package cm_omc

import (
	"fmt"

	"be.ems/restagent/config"
)

const (
	PASSWORD_MASK = "********"
)

func (o *ConfigOMC) Query(paramName string) (any, error) {
	var results []any

	switch paramName {
	case "alarmEmailForward":
		result := config.GetYamlConfig().Alarm.EmailForward
		result.Password = PASSWORD_MASK
		results = append(results, result)
	case "alarmSMSForward":
		result := config.GetYamlConfig().Alarm.SMSCForward
		result.Password = PASSWORD_MASK
		results = append(results, result)
	default:
		return nil, fmt.Errorf("invalid source parameter")
	}

	return results, nil
}

func (o *ConfigOMC) Add() {

}

func (o *ConfigOMC) Modify(paramName string, paramData map[string]any) (any, error) {
	var results []any

	switch paramName {
	case "alarmEmailForward":
		param := &(config.GetYamlConfig().Alarm.EmailForward)
		config.UpdateStructFromMap(param, paramData)
		result := *param
		results = append(results, result)
		err := config.WriteOrignalConfig(config.YamlConfigInfo.FilePath, paramName, paramData)
		if err != nil {
			fmt.Println("failed to write config yaml file:", err)
			return results, err
		}
	case "alarmSMSForward":
		param := &(config.GetYamlConfig().Alarm.SMSCForward)
		config.UpdateStructFromMap(param, paramData)
		result := *param
		results = append(results, result)
		err := config.WriteOrignalConfig(config.YamlConfigInfo.FilePath, paramName, paramData)
		if err != nil {
			fmt.Println("failed to write config yaml file:", err)
			return results, err
		}
	default:
		return nil, fmt.Errorf("invalid source parameter")
	}

	return results, nil
}

func (o *ConfigOMC) Remove() {

}
