package config

import (
	"flag"
	"os"
)

// Slice-2: the control plane listens on an unusual port to avoid
// collisions with anything else on the VM. Nginx on 186 proxies
// /sandbox/credit-card/api/ and /sandbox/credit-card/ws/ to it.
const defaultAddr = ":3452"

type Config struct {
	Addr        string // listen addr, default :3452
	DataDir     string // SQLite + .log files live here
	AgentHealth string // map of nodeID -> agent base URL (TODO: real map)
}

// Load reads flags and env. Env wins over defaults; flags win over env.
func Load() Config {
	c := Config{
		Addr:        envOr("SDP_ADDR", defaultAddr),
		DataDir:     envOr("SDP_DATA", "./data"),
		AgentHealth: envOr("SDP_AGENT_HEALTH", ""),
	}
	flag.StringVar(&c.Addr, "addr", c.Addr, "control plane listen addr")
	flag.StringVar(&c.DataDir, "data", c.DataDir, "data directory for sqlite and logs")
	flag.Parse()
	return c
}

func envOr(k, def string) string {
	if v := os.Getenv(k); v != "" {
		return v
	}
	return def
}
