extends Area2D

# Stairs that trigger level completion when player enters

@onready var sfx_stairs = null

func _ready():
	# Connect body entered signal
	body_entered.connect(_on_body_entered)
	
	# Set collision layer/mask to detect players
	collision_layer = 0
	collision_mask = 1  # Detect players (layer 1)
	
	# Load stairs sound effect
	if not sfx_stairs:
		# Try to create AudioStreamPlayer2D if it doesn't exist
		sfx_stairs = AudioStreamPlayer2D.new()
		sfx_stairs.name = "SfxStairs"
		add_child(sfx_stairs)
	
	# Load go_down_stairs.mp3 sound
	var stairs_sound = load("res://assets/audio/sfx/walk/go_down_stairs.mp3")
	if stairs_sound:
		sfx_stairs.stream = stairs_sound
	else:
		print("Stairs: Warning - Could not load go_down_stairs.mp3")

func _on_body_entered(body: Node2D):
	print("Stairs: Body entered: ", body, " is_player: ", body.is_in_group("player") if body else false, " is_dead: ", body.is_dead if body and "is_dead" in body else false)
	if body and body.is_in_group("player") and not body.is_dead:
		print("Stairs: Player entered stairs! Player: ", body.name)
		
		# Play stairs sound effect
		if sfx_stairs and sfx_stairs.stream:
			sfx_stairs.play()
		
		# Only trigger on server/authority
		if multiplayer.is_server() or not multiplayer.has_multiplayer_peer():
			print("Stairs: Server detected, calling game_world")
			var game_world = get_tree().get_first_node_in_group("game_world")
			if game_world:
				print("Stairs: Game world found, calling _on_player_reached_stairs")
				game_world._on_player_reached_stairs(body)
			else:
				print("Stairs: ERROR - Game world not found!")
		else:
			print("Stairs: Not server, ignoring")
