package config

import (
	"encoding/json"
	"os"
)

// Config holds wgctl-conntrack runtime configuration
type Config struct {
	WGSubnet      string
	DataDir       string
	ClientsDir    string
	AcceptLogFile string
	ServicesFile  string
}

type wgctlJSON struct {
	WireGuard struct {
		Subnet string `json:"subnet"`
	} `json:"wireguard"`
}

// Load reads config from wgctl.json and applies defaults
func Load(wgDir string) (*Config, error) {
	cfg := &Config{
		WGSubnet:      "10.1.0.0/16",
		DataDir:       wgDir + "/.wgctl/data",
		ClientsDir:    wgDir + "/clients",
		AcceptLogFile: wgDir + "/.wgctl/daemon/accept_events.log",
		ServicesFile:  wgDir + "/.wgctl/data/services.json",
	}

	jsonFile := wgDir + "/.wgctl/config/wgctl.json"
	if data, err := os.ReadFile(jsonFile); err == nil {
		var wj wgctlJSON
		if json.Unmarshal(data, &wj) == nil && wj.WireGuard.Subnet != "" {
			cfg.WGSubnet = wj.WireGuard.Subnet
		}
	}

	return cfg, nil
}