package domain

import (
	"log"
	"os"

	"github.com/joho/godotenv"
)

type Config struct {
	Git      GitConfig
	Cloud    CloudConfig
	Coda     CodaConfig
	DB       DBConfig
	Telegram TelegramConfig
	Discord  DiscordConfig
}

type GitConfig struct {
	BaseUrl string
	Token   string
	User    string
	OrgName string
	Team    string
}

type CloudConfig struct {
	BaseUrl string
	User    string
	Pass    string
	RootDir string
}

type CodaConfig struct {
	Farm    string
	Develop string
}

type DBConfig struct {
	Host    string
	Port    string
	Name    string
	User    string
	Pass    string
	SslMode string
}

type TelegramConfig struct {
	Token string
}

type DiscordConfig struct {
	Token            string
	IsProjectChannel string
	IsTaskForum      string
	Tags             map[TaskState]string
}

type ApplicationConfig struct {
	Key string
	ID  int
}

// InitConfig
// InitConfig function reads provided file and setup envirmental variables;
func InitConfig(env string) Config {
	log.Printf("loading ENV from: %s", env)
	err := godotenv.Load(env)
	if err != nil {
		log.Fatal("Error while loading env file")
	}

	return Config{
		Git: GitConfig{
			BaseUrl: os.Getenv("GIT_BASE_URL"),
			Token:   os.Getenv("GIT_TOKEN"),
			User:    os.Getenv("GIT_USER"),
			OrgName: os.Getenv("ORG"),
			Team:    os.Getenv("TEAM"),
		},
		Cloud: CloudConfig{
			BaseUrl: os.Getenv("CLOUD_BASE_URL"),
			User:    os.Getenv("CLOUD_USER"),
			Pass:    os.Getenv("CLOUD_PASS"),
			RootDir: os.Getenv("ROOTDIR"),
		},
		Coda: CodaConfig{
			Farm:    os.Getenv("CODA_TOKEN1"),
			Develop: os.Getenv("CODA_TOKEN2"),
		},
		DB: DBConfig{
			Host:    os.Getenv("DB_HOST"),
			Port:    os.Getenv("DB_PORT"),
			Name:    os.Getenv("DB_NAME"),
			User:    os.Getenv("DB_USER"),
			Pass:    os.Getenv("DB_PASS"),
			SslMode: os.Getenv("SSLMODE"),
		},
		Telegram: TelegramConfig{
			Token: os.Getenv("TG_API"),
		},
		Discord: DiscordConfig{
			Token:            os.Getenv("DISCORD_TOKEN"),
			IsProjectChannel: os.Getenv("PROJECTS_CHANNEL_GROUP"),
			IsTaskForum:      os.Getenv("TASKS_CHANNEL"),
		},
	}
}
