package configs

import (
	"crm-api/pkg/lfshook"
	"encoding/json"
	"io/ioutil"
)

type PushConfig struct {
	AMIEvents  []string `json:"events"`
	AMIPushUrl string   `json:"allEventPushUrl"`
}

var PushConfigValue PushConfig

func GetPushConfig() {
	data, err := ioutil.ReadFile(ConfigGlobal.PushConfig)
	if err != nil {
		PushConfigValue = PushConfig{
			AMIEvents:  []string{},
			AMIPushUrl: "",
		}
		return
	}
	json.Unmarshal(data, &PushConfigValue)
}

func UpdatePushConfig(config PushConfig) error {
	event, err := json.Marshal(config)
	if err != nil {
		lfshook.NewLogger().Error(err)
		return err
	}
	err = ioutil.WriteFile(ConfigGlobal.PushConfig, event, 666)
	if err != nil {
		lfshook.NewLogger().Error(err)
		return err
	}
	PushConfigValue = config
	return nil
}
