extends RefCounted
class_name APIResponse
## API Response Model
##
## Represents a response from the Flask backend API.
## Matches the backend response format:
## {
##   "app": "Code of Conquest",
##   "version": "0.1.0",
##   "status": 200,
##   "timestamp": "2025-11-16T...",
##   "result": {...},
##   "error": {...},
##   "meta": {...}
## }

var app: String
var version: String
var status: int
var timestamp: String
var result: Variant
var error: Dictionary
var meta: Dictionary
var raw_response: String


func _init(json_data: Dictionary) -> void:
	app = json_data.get("app", "")
	version = json_data.get("version", "")
	status = json_data.get("status", 0)
	timestamp = json_data.get("timestamp", "")
	result = json_data.get("result", null)

	# Handle Dictionary fields that might be null
	var error_data = json_data.get("error", null)
	error = error_data if error_data != null else {}

	var meta_data = json_data.get("meta", null)
	meta = meta_data if meta_data != null else {}


## Check if the request was successful (2xx status)
func is_success() -> bool:
	return status >= 200 and status < 300


## Check if there's an error
func has_error() -> bool:
	return not error.is_empty() or status >= 400


## Get error message if present
func get_error_message() -> String:
	if error.has("message"):
		return error["message"]
	return ""
