extends RefCounted
## Tutorial quests per roadmap unlock (Boss 2026-10-03/04, agent B): the teaching side of agent A's registry.
## Rules + state: server/unlocks.gd (agent A) - a feature is open when level >= lv AND its tutorial is done (tester
## switch / grandfathered saves open everything); its tutorial is completed on the SERVER by the command
## {"t": "tut_done", "key": k} (unlocks.gd command(): level checked, idempotent, grants the gacha free pulls, the first
## mount, the free outfit) or, for the quest-only features (Awakening / Asura Rider), by turning in their real quest.
## Teaching NPC per feature = unlocks.json "npc" (one source of truth with feature_why()).
## This file adds the lesson data (data/tutorials.json: one row per feature key - card text, painted ring icon, slides,
## gacha window to open) and the small queries the HUD cards ask (ui/tutorial_ui.gd).

const DATA := "res://data/tutorials.json"

static var _defs: Array = []
static var _by_id: Dictionary = {}


static func U():
	return load("res://scripts/server/unlocks.gd")


static func defs() -> Array:
	if _defs.is_empty():
		var f := FileAccess.open(DATA, FileAccess.READ)
		var d = JSON.parse_string(f.get_as_text()) if f != null else null
		if d is Dictionary:
			_defs = d.get("tutorials", [])
		for t in _defs:
			_by_id[str(t.get("id", ""))] = t
	return _defs


## Lesson row of feature `id` ({} = none).
static func def(id: String) -> Dictionary:
	defs()
	return _by_id.get(id, {})


## Teaching NPC id (the registry's, else the row's fallback).
static func npc(id: String) -> String:
	var n := str(U().feature(id).get("npc", ""))
	return n if n != "" else str(def(id).get("npc", ""))


static func level(t: Dictionary) -> int:
	var lv := int(U().feature_level(str(t.get("id", ""))))
	return lv if lv > 0 else int(t.get("lv", 1))


## Awakening / Asura Rider: taught here, completed by turning in their real quest.
static func quest_only(id: String) -> bool:
	return bool(U().feature(id).get("quest_only", false))


static func quest_of(id: String) -> String:
	return str(U().tutorial_of(id))


## Pending lessons (level reached, tutorial not done; never with the tester switch / for grandfathered features),
## lowest level first - one HUD card each.
static func pending(ch) -> Array:
	if ch == null or U().tester() or not U().gates_on("mount"):
		# tester switch / roadmap gates off (config roadmap_gates false, e.g. the old selftest suites): no lessons
		return []
	var out: Array = []
	var ks: Array = U().pending_tutorials(ch)
	for k in ks:
		# Boss 2026-10-09: both Awakening lessons sit at Lv 40 with the same teacher - the hand-off lesson
		# (awaken_preview: the ULT awakening + skins) comes first, then the Awakening Trial quest card
		if str(k) == "awaken" and "awaken_preview" in ks:
			continue
		if not def(str(k)).is_empty():
			out.append(str(k))
	return out


static func is_pending(ch, id: String) -> bool:
	return id in pending(ch)


static func done(ch, id: String) -> bool:
	return ch != null and bool(U().tutorial_done(ch, id))


## The pending lesson this NPC teaches that should open its slide book ("" = none). A quest-only lesson whose quest is
## already taken (or done) lets the NPC's normal dialog run (turn-in).
static func pending_at(ch, npc_id: String) -> String:
	if ch == null or npc_id == "":
		return ""
	for k in pending(ch):
		if npc(k) != npc_id:
			continue
		if quest_only(k):
			var ql = ch.get("quests")
			var q := quest_of(k)
			if ql != null and (ql.is_active(q) or ql.is_done(q)):
				continue
		return k
	return ""
