class_name GoalData
extends Resource

enum UnlockBehavior {
	AUTOMATIC,
	MANUAL,
}

@export var id: StringName = &""
@export var requirements: Array[GoalRequirementData] = []
@export var unlock_behavior: UnlockBehavior = UnlockBehavior.MANUAL

func has_id() -> bool:
	return not String(id).strip_edges().is_empty()

func get_valid_requirements() -> Array[GoalRequirementData]:
	var result: Array[GoalRequirementData] = []
	for requirement_resource in requirements:
		if requirement_resource == null:
			continue
		if not bool(requirement_resource.is_valid()):
			continue

		result.append(requirement_resource)

	return result

func is_valid() -> bool:
	if not has_id():
		return false

	return not get_valid_requirements().is_empty()

func is_met() -> bool:
	var valid_requirements: Array[GoalRequirementData] = get_valid_requirements()
	if valid_requirements.is_empty():
		return false

	for requirement_resource in valid_requirements:
		if not bool(requirement_resource.is_met()):
			return false

	return true

func get_first_requirement_summary() -> String:
	var valid_requirements: Array[GoalRequirementData] = get_valid_requirements()
	if valid_requirements.is_empty():
		return ""
	return String(valid_requirements[0].get_summary_text())

func get_progress() -> float:
	var valid_requirements: Array[GoalRequirementData] = get_valid_requirements()
	if valid_requirements.is_empty():
		return 0.0
	
	var min_progress: float = 1.0
	for requirement in valid_requirements:
		if requirement == null or not requirement.is_valid():
			continue
		
		var requirement_progress: float = requirement.get_progress()
		if requirement_progress < min_progress:
			min_progress = requirement_progress
	
	return min_progress
