package config

import (
	"errors"
	"io/fs"
	"os"
	"path/filepath"

	"github.com/BurntSushi/toml"
)

type Config struct {
	Theme        string              `toml:"theme"`
	AutoCheck    bool                `toml:"autoCheck"`
	TimerEnabled bool                `toml:"timerEnabled"`
	Bindings     map[string][]string `toml:"bindings"`
}

func Default() Config {
	return Config{
		Theme:        "dark",
		AutoCheck:    true,
		TimerEnabled: true,
		Bindings:     map[string][]string{},
	}
}

func path() (string, error) {
	h, err := os.UserHomeDir()
	if err != nil {
		return "", err
	}
	return filepath.Join(h, ".termdoku", "config.toml"), nil
}

func Load() (Config, error) {
	cfg := Default()
	p, err := path()
	if err != nil {
		return cfg, err
	}
	b, err := os.ReadFile(p)
	if err != nil {
		if errors.Is(err, fs.ErrNotExist) {
			return cfg, nil
		}
		return cfg, err
	}
	if err := toml.Unmarshal(b, &cfg); err != nil {
		return cfg, err
	}
	return cfg, nil
}

func Save(cfg Config) error {
	p, err := path()
	if err != nil {
		return err
	}
	if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
		return err
	}
	data, err := toml.Marshal(cfg)
	if err != nil {
		return err
	}
	return os.WriteFile(p, data, 0o644)
}
