extends Control
## Character List Screen
##
## Displays user's characters and allows selection or creation.
## Fetches data from GET /api/v1/characters endpoint.

#region Signals
signal character_selected(character_id: String)
signal create_requested
#endregion

#region Node References
@onready var character_container: VBoxContainer = $VBoxContainer/CharacterScrollContainer/CharacterVBox
@onready var loading_indicator: CenterContainer = $VBoxContainer/LoadingIndicator
@onready var error_container: MarginContainer = $VBoxContainer/ErrorContainer
@onready var error_label: Label = $VBoxContainer/ErrorContainer/ErrorLabel
@onready var empty_state: CenterContainer = $EmptyState
@onready var tier_label: Label = $VBoxContainer/Header/TierLabel
@onready var create_button: Button = $VBoxContainer/ActionContainer/CreateButton
@onready var refresh_button: Button = $VBoxContainer/ActionContainer/RefreshButton
@onready var scroll_container: ScrollContainer = $VBoxContainer/CharacterScrollContainer
@onready var empty_create_button: Button = $EmptyState/VBoxContainer/CreateFirstButton
#endregion

#region Service References
@onready var http_client: Node = get_node("/root/HTTPClient")
@onready var state_manager: Node = get_node("/root/StateManager")
#endregion

#region Private Variables
var _characters: Array = []
var _is_loading: bool = false
var _tier: String = "free"
var _limit: int = 1
#endregion

#region Lifecycle
func _ready() -> void:
	print("[CharacterList] Initialized")
	_connect_signals()
	_hide_error()
	empty_state.visible = false
	load_characters()


func _connect_signals() -> void:
	create_button.pressed.connect(_on_create_button_pressed)
	refresh_button.pressed.connect(_on_refresh_button_pressed)
	empty_create_button.pressed.connect(_on_create_button_pressed)
#endregion

#region Public Methods
func load_characters() -> void:
	"""Fetch characters from API."""
	if _is_loading:
		return

	print("[CharacterList] Loading characters...")
	_set_loading(true)
	_hide_error()

	http_client.http_get(
		"/api/v1/characters",
		_on_characters_loaded,
		_on_characters_error
	)


func refresh() -> void:
	"""Refresh character list."""
	load_characters()
#endregion

#region Private Methods
func _set_loading(loading: bool) -> void:
	_is_loading = loading
	loading_indicator.visible = loading
	scroll_container.visible = not loading
	refresh_button.disabled = loading


func _show_error(message: String) -> void:
	error_label.text = message
	error_container.visible = true


func _hide_error() -> void:
	error_container.visible = false


func _update_ui() -> void:
	# Update tier info
	tier_label.text = "%s: %d/%d" % [_tier.capitalize(), _characters.size(), _limit]

	# Enable/disable create button based on limit
	create_button.disabled = _characters.size() >= _limit

	# Show empty state or character list
	if _characters.is_empty():
		empty_state.visible = true
		scroll_container.visible = false
	else:
		empty_state.visible = false
		scroll_container.visible = true


func _populate_character_list() -> void:
	# Clear existing cards
	for child in character_container.get_children():
		child.queue_free()

	# Create card for each character
	var card_scene = preload("res://scenes/character/character_card.tscn")

	for char_data in _characters:
		var card = card_scene.instantiate()
		card.set_character_data(char_data)
		card.selected.connect(_on_character_card_selected)
		card.delete_requested.connect(_on_character_delete_requested)
		character_container.add_child(card)

	print("[CharacterList] Populated %d character cards" % _characters.size())
#endregion

#region Signal Handlers
func _on_characters_loaded(response: APIResponse) -> void:
	_set_loading(false)

	if not response.is_success():
		_on_characters_error(response)
		return

	var result = response.result
	_characters = result.get("characters", [])
	_tier = result.get("tier", "free")
	_limit = result.get("limit", 1)

	print("[CharacterList] Loaded %d characters (tier=%s, limit=%d)" % [_characters.size(), _tier, _limit])

	_populate_character_list()
	_update_ui()


func _on_characters_error(response: APIResponse) -> void:
	_set_loading(false)

	var message = response.get_error_message()
	if message.is_empty():
		message = "Failed to load characters"

	if response.status == 401:
		print("[CharacterList] Session expired, redirecting to login")
		get_tree().change_scene_to_file("res://scenes/auth/login.tscn")
		return

	print("[CharacterList] Error: %s (status=%d)" % [message, response.status])
	_show_error(message)


func _on_character_card_selected(character_id: String) -> void:
	print("[CharacterList] Character selected: %s" % character_id)
	state_manager.select_character(character_id)
	character_selected.emit(character_id)
	# TODO: Navigate to main game scene
	# get_tree().change_scene_to_file("res://scenes/game/main_game.tscn")


func _on_character_delete_requested(character_id: String) -> void:
	print("[CharacterList] Delete requested for: %s" % character_id)
	# TODO: Show confirmation dialog, then call DELETE endpoint
	# For now, directly delete
	_delete_character(character_id)


func _delete_character(character_id: String) -> void:
	"""Delete a character via API."""
	http_client.http_delete(
		"/api/v1/characters/%s" % character_id,
		func(response: APIResponse):
			if response.is_success():
				print("[CharacterList] Character deleted: %s" % character_id)
				load_characters()  # Refresh list
			else:
				_show_error(response.get_error_message()),
		func(response: APIResponse):
			_show_error(response.get_error_message())
	)


func _on_create_button_pressed() -> void:
	print("[CharacterList] Create button pressed")
	create_requested.emit()
	# TODO: Navigate to character creation wizard
	# get_tree().change_scene_to_file("res://scenes/character/character_create.tscn")


func _on_refresh_button_pressed() -> void:
	print("[CharacterList] Refresh button pressed")
	refresh()
#endregion
