package config

import (
	"github.com/BurntSushi/toml"
	"github.com/rs/zerolog/log"
	"os"
	"time"
)

type config struct {
	APIToken        string
	DebugMode       bool
	PrivilegedUsers []int64
	Shutup          map[int64]time.Time //is always filled at startup, not present in config
	Webserver struct {
		Port string
	}
	Balonlyl        struct {
		EnabledChats []int64
		BalonlylAT   string
		TriggerWords []string
	}
	GayPoints struct {
		EnabledChats []int64
		ModifyUsers  []int64
	}
	Download struct {
		Prefix string
		Yourls struct {
			APIPath   string
			Signature string
		}
	}
}

var BotConfig config

func LoadConfig() {

	configFile, err := os.ReadFile("config.toml")
	if err != nil {
		log.Panic().Err(err).Msg("rror opening config file")
	}

	_, err = toml.Decode(string(configFile), &BotConfig)
	if err != nil {
		log.Panic().Err(err).Msg("error decoding config file")
	}

	BotConfig.Shutup = make(map[int64]time.Time)

	log.Info().Msg("loaded config file")
	log.Debug().Interface("config", BotConfig).Msg("")

}

func EncodeToml() {

	exampleConfig := config{
		APIToken:        "1234567:h8Y7ma7hMG93kxDx5o",
		DebugMode:       false,
		PrivilegedUsers: []int64{1234567,9764382423},
		Webserver:       struct{ Port string }{
			Port: "3000",
		},
		Balonlyl:        struct {
			EnabledChats []int64
			BalonlylAT   string
			TriggerWords []string
		}{
			EnabledChats: []int64{353454534,8743658},
			BalonlylAT:   "@Balonlyl",
			TriggerWords: []string{"French, France"},
		},
		GayPoints:       struct {
			EnabledChats []int64
			ModifyUsers  []int64
		}{
			EnabledChats: []int64{353454534,8743658},
			ModifyUsers:  []int64{3827468324,1736576},
		},
		Download:        struct {
			Prefix string
			Yourls struct {
				APIPath   string
				Signature string
			}
		}{
			Prefix: "https://bloater.example.com",
			Yourls: struct {
				APIPath   string
				Signature string
			}{
				APIPath:   "https://short.com/yourls-api.php",
				Signature: "kfn83h28r",
			},
		},
	}



	f, err := os.Create("config.toml.example")
if err != nil {
    log.Panic().Err(err).Msg("error encoding config file. Cannot open config.toml.example")
}

if err := toml.NewEncoder(f).Encode(exampleConfig); err != nil {
    log.Panic().Err(err).Msg("error encoding config file")
}
if err := f.Close(); err != nil {
	log.Panic().Err(err).Msg("error encoding config file. Cannot close config.toml.example")

}

log.Info().Msg("Wrote example config to config.toml.example. Bye Bye")

}