package config

import (
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	ServerAddress     string `yaml:"server_address"`
	AgentID           string `yaml:"agent_id"`
	HeartbeatInterval int    `yaml:"heartbeat_interval"`
	TLSEnabled        bool   `yaml:"tls_enabled"`
}

func Load(path string) (*Config, error) {
	cfg := &Config{
		ServerAddress:     "localhost:5001",
		HeartbeatInterval: 60,
	}
	data, err := os.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			return cfg, nil
		}
		return nil, err
	}
	if err := yaml.Unmarshal(data, cfg); err != nil {
		return nil, err
	}
	return cfg, nil
}

func (c *Config) Save(path string) error {
	data, err := yaml.Marshal(c)
	if err != nil {
		return err
	}
	return os.WriteFile(path, data, 0600)
}
