package provision

import (
	"os"
	"strings"

	"zlh-agent/internal/provcommon"
	"zlh-agent/internal/state"
)

const (
	ServersRoot  = provcommon.ServersRoot
	JavaRoot     = provcommon.JavaRoot
	SteamCMDPath = provcommon.SteamCMDPath
)

func ServerDir(cfg state.Config) string {
	return provcommon.ServerDir(cfg)
}

func JavaDir(cfg state.Config) string {
	return provcommon.JavaDir(cfg)
}

func BuildArtifactURL(path string) string {
	return provcommon.BuildArtifactURL(path)
}

func ExpectedMinecraftJar(cfg state.Config) string {
	return "server.jar"
}

func MinecraftRuntime(cfg state.Config) string {
	if runtime := strings.ToLower(strings.TrimSpace(cfg.Runtime)); runtime != "" {
		return runtime
	}
	return strings.ToLower(strings.TrimSpace(cfg.Variant))
}

// Local fallback used ONLY inside this package — NOT exported.
func buildArtifactURL(path string) string {
	if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
		return path
	}

	base := os.Getenv("ZLH_ARTIFACT_BASE_URL")
	if base == "" {
		base = "http://zlh-artifacts.internal.zlh:8080/" // fallback
	}

	return strings.TrimRight(base, "/") + "/" + strings.TrimLeft(path, "/")
}

func IsSteamGame(game string) bool {
	g := strings.ToLower(game)
	switch g {
	case "valheim", "rust", "terraria", "projectzomboid":
		return true
	}
	return false
}
