package config

import (
	"os"
	"strconv"
	"time"
)

type Config struct {
	ListenAddr      string
	APIURL          string
	EdgeKey         string
	PlayDomain      string
	StartWait       time.Duration
	PollInterval    time.Duration
}

func Load() Config {
	startWaitSec, _ := strconv.Atoi(envOr("EDGE_START_WAIT_SECONDS", "180"))
	pollMs, _ := strconv.Atoi(envOr("EDGE_POLL_INTERVAL_MS", "2000"))

	return Config{
		ListenAddr:   envOr("EDGE_LISTEN_ADDR", ":25565"),
		APIURL:       envOr("API_URL", "http://localhost:3001"),
		EdgeKey:      envOr("EDGE_INTERNAL_API_KEY", "local-dev-edge-key-change-me-32chars"),
		PlayDomain:   envOr("GAME_BASE_DOMAIN", "play.example.net"),
		StartWait:    time.Duration(startWaitSec) * time.Second,
		PollInterval: time.Duration(pollMs) * time.Millisecond,
	}
}

func envOr(key, fallback string) string {
	if value := os.Getenv(key); value != "" {
		return value
	}
	return fallback
}
