@tool
extends RefCounted
## Golem settings — stored in EditorSettings (per-user, not per-project).

const PROVIDERS := {
	"openai": {"name": "OpenAI", "base_url": "https://api.openai.com/v1", "default_model": "gpt-4o"},
	"openrouter": {"name": "OpenRouter", "base_url": "https://openrouter.ai/api/v1", "default_model": "anthropic/claude-sonnet-4"},
	"ollama": {"name": "Ollama (local)", "base_url": "http://localhost:11434/v1", "default_model": "llama3"},
	"gemini": {"name": "Google Gemini", "base_url": "https://generativelanguage.googleapis.com/v1beta/openai", "default_model": "gemini-2.0-flash"},
	"custom": {"name": "Custom", "base_url": "", "default_model": ""},
}

const _PREFIX := "golem/"


static func get_api_key() -> String:
	return str(_read("api_key", ""))


static func set_api_key(value: String) -> void:
	_write("api_key", value)


static func get_provider() -> String:
	return str(_read("provider", "openai"))


static func set_provider(value: String) -> void:
	_write("provider", value)


static func get_model() -> String:
	var provider := get_provider()
	var default_model: String = PROVIDERS.get(provider, {}).get("default_model", "gpt-4o")
	return str(_read("model", default_model))


static func set_model(value: String) -> void:
	_write("model", value)


static func get_base_url() -> String:
	var provider := get_provider()
	var default_url: String = PROVIDERS.get(provider, {}).get("base_url", "https://api.openai.com/v1")
	return str(_read("base_url", default_url))


static func set_base_url(value: String) -> void:
	_write("base_url", value)


static func get_temperature() -> float:
	return float(_read("temperature", 0.7))


static func set_temperature(value: float) -> void:
	_write("temperature", value)


static func is_configured() -> bool:
	if get_base_url().is_empty():
		return false
	var provider := get_provider()
	if provider == "ollama":
		return true  # No API key needed
	return not get_api_key().is_empty()


static func _read(key: String, default_value: Variant) -> Variant:
	var es := EditorInterface.get_editor_settings()
	var full_key := _PREFIX + key
	if es.has_setting(full_key):
		return es.get_setting(full_key)
	return default_value


static func _write(key: String, value: Variant) -> void:
	var es := EditorInterface.get_editor_settings()
	es.set_setting(_PREFIX + key, value)
