extends PathFollow3D
## Tower Defense — Lecture 26 (Starter)
##
## Enemy that follows a Path3D, has health, and damages
## the player when reaching the end. Uses colored shapes
## for visualization.

# ============================================================
# SIGNALS
# ============================================================

signal reached_end(damage: int)
signal died()

# ============================================================
# ENEMY STATS (set based on type)
# ============================================================

const STATS := {
	"slime": {
		"hp": 30,
		"speed": 2.0,
		"damage": 10,
		"color": Color(0.2, 0.8, 0.2)  # Green
	},
	"goblin": {
		"hp": 50,
		"speed": 3.5,
		"damage": 15,
		"color": Color(0.8, 0.6, 0.2)  # Orange
	},
	"orc": {
		"hp": 100,
		"speed": 2.5,
		"damage": 25,
		"color": Color(0.8, 0.2, 0.2)  # Red
	}
}

# ============================================================
# STATE
# ============================================================

var enemy_type: String = "slime"
var max_hp: int = 30
var current_hp: int = 30
var speed: float = 2.0
var damage: int = 10
var enemy_color: Color = Color.GREEN

@onready var mesh_instance: MeshInstance3D = $MeshInstance3D
@onready var health_bar: MeshInstance3D = $HealthBar
@onready var health_bar_fill: MeshInstance3D = $HealthBar/Fill

# ============================================================
# LIFECYCLE
# ============================================================

func _ready() -> void:
	loop = false


func _process(delta: float) -> void:
	# Move along path
	progress += speed * delta
	
	# Check if reached end
	if progress_ratio >= 1.0:
		_reach_end()


## Initialize enemy with stats based on type
func initialize() -> void:
	# === TODO 1: Apply stats from STATS dictionary ===
	# Check if enemy_type exists in STATS (default to "slime" if not)
	# Extract stats dict for this enemy type
	# Set max_hp, current_hp, speed, damage, and enemy_color from stats
	# Create a StandardMaterial3D and set its albedo_color to enemy_color
	# Apply material to mesh_instance using set_surface_override_material(0, material)
	# Call _update_health_bar()
	pass


# ============================================================
# COMBAT
# ============================================================

## Take damage from tower
func take_damage(amount: int) -> void:
	current_hp -= amount
	current_hp = maxi(current_hp, 0)
	_update_health_bar()
	
	if current_hp <= 0:
		_die()


## Enemy dies
func _die() -> void:
	died.emit()
	queue_free()


## Enemy reaches end of path
func _reach_end() -> void:
	reached_end.emit(damage)
	queue_free()


# ============================================================
# HEALTH BAR
# ============================================================

func _update_health_bar() -> void:
	# === TODO 2: Update health bar visualization ===
	# Calculate health_percent as float(current_hp) / float(max_hp)
	# Set health_bar_fill.scale.x to health_percent
	# Lerp bar color from GREEN (full health) to RED (no health)
	#   Use: Color.GREEN.lerp(Color.RED, 1.0 - health_percent)
	# Create StandardMaterial3D with the lerped color
	# Apply to health_bar_fill using set_surface_override_material(0, material)
	pass
