class_name ChopProvider extends WorkProvider
## WorkProvider for the "chop" work category.
##
## Scans World.trees for the nearest choppable Tree (Manhattan distance from
## the requesting pawn) and returns a two-toil Job:
##   walk_to(tree.tile) → interact(tree.get_path(), "on_chop_tick")
##
## The INTERACT toil calls Tree.on_chop_tick() once per sim tick; the Tree
## internally tracks chop_progress and calls fell() when CHOP_TICKS is
## reached.  The toil finishes automatically when is_choppable() returns
## false (felled) or when the node is freed.
##
## Reachability: trees are impassable, so the pawn stands at a walkable tile
## adjacent to the tree. A reachability pre-check on that adjacent tile prevents
## a busy-spin when all approach tiles are blocked.
##
## Duck-typing note: Tree is referenced without class_name (class may not be
## registered yet when this provider loads).  We rely only on:
##   tree.tile: Vector2i
##   tree.is_choppable() -> bool
##   tree.get_path() -> NodePath


func _init() -> void:
	category = &"chop"
	priority = 5  # Higher than rest (priority 0); scanned before it by Decision.


## Returns a Job targeting the nearest choppable Tree, or null if none exists.
## `pawn` is duck-typed: must expose .tile (Vector2i).
func find_best_for(pawn) -> Job:
	var best = null
	var best_dist: int = 999999

	for tree in World.trees:
		if not tree.is_choppable():
			continue
		# Gate on player designation — pawns don't auto-chop undesignated trees.
		# Boot seed in world.gd auto-designates SAMPLE_TREES so the demo still runs.
		if not tree.chop_designated:
			continue
		if Job.is_target_taken_by_other(tree, pawn):
			continue
		# Reachability pre-check — trees are impassable, pawn approaches from adjacent
		# tile. Mirrors ConstructionProvider Pattern B. Skip if no walkable neighbour
		# exists or none is reachable.
		var approach: Vector2i = _find_adjacent_walkable(tree.tile, pawn.tile)
		if approach == tree.tile:
			continue  # no walkable neighbour at all
		if pawn.tile != approach and World.pathfinder.find_path(pawn.tile, approach).is_empty():
			continue
		var d: int = abs(tree.tile.x - pawn.tile.x) + abs(tree.tile.y - pawn.tile.y)
		if d < best_dist:
			best_dist = d
			best = tree

	if best == null:
		return null

	var j := Job.new()
	j.label = "Chop tree at %s" % best.tile
	j.target_node = best
	# Walk to adjacent walkable tile — trees are impassable.
	var stand_tile: Vector2i = _find_adjacent_walkable(best.tile, pawn.tile)
	j.toils.append(Toil.walk_to(stand_tile))
	j.toils.append(Toil.interact(best.get_path(), &"on_chop_tick"))
	return j


# ── helpers ──────────────────────────────────────────────────────────────────

## Finds the 4-neighbour of `target` nearest to `prefer_near` that the pathfinder
## currently treats as walkable. Returns `target` itself if no walkable neighbour
## exists (caller treats that as "no approach available").
## Mirrors ConstructionProvider._find_adjacent_walkable.
func _find_adjacent_walkable(target: Vector2i, prefer_near: Vector2i) -> Vector2i:
	var offsets: Array[Vector2i] = [
		Vector2i(0, -1), Vector2i(1, 0), Vector2i(0, 1), Vector2i(-1, 0),
	]
	var best: Vector2i = target
	var best_dist: int = 999999
	for off in offsets:
		var t: Vector2i = target + off
		if World.pathfinder == null:
			continue
		if not World.pathfinder.is_walkable(t):
			continue
		var d: int = abs(t.x - prefer_near.x) + abs(t.y - prefer_near.y)
		if d < best_dist:
			best_dist = d
			best = t
	return best
