package config

import (
	"os"

	providerConfigs "git.maurice.fr/thomas/mailout/pkg/providers/configs"
	"gopkg.in/yaml.v3"
)

type Config struct {
	Postgres struct {
		Hostname string `yaml:"hostname"`
		Port     int    `yaml:"port"`
		User     string `yaml:"user"`
		Password string `yaml:"password"`
		Database string `yaml:"database"`
		SSLMode  string `yaml:"sslmode"`
	} `yaml:"postgres"`
	DefaultProvider string `yaml:"defaultProvider"`
	Providers       struct {
		OVH *providerConfigs.OVHConfig `yaml:"ovh"`
	} `yaml:"providers"`
}

func LoadConfig(path string) (*Config, error) {
	b, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	var cfg Config
	err = yaml.Unmarshal(b, &cfg)
	if err != nil {
		return nil, err
	}

	if cfg.Postgres.SSLMode == "" {
		cfg.Postgres.SSLMode = "disable"
	}

	if cfg.Postgres.Port == 0 {
		cfg.Postgres.Port = 5432
	}

	return &cfg, err
}