# ============================================================
# The Handcrafter — Unified interface (the Tome's right-click page)
# ============================================================
# A clean tellraw page reached by right-clicking the Handcrafter's Tome (book/on_use)
# OR the W4 Grand-Quests "Build" row could route here later. Shows, in one place:
#   1. the 13-tier Handcrafter rank LADDER (current rank highlighted) — render_ladder
#   2. next-rank PROGRESS (ec.bc_credit toward the bc/threshold need)
#   3. the unlocked permanent PERKS (rank-gated: Reach / Resourceful Hands / Sure
#      Footing / Tireless Mason)
#   4. the active BUILD QUEST (reads ec.bq_* — the line started by on_first_place)
#
# CONTEXT-AGNOSTIC + IDEMPOTENT (R8): pure tellraw helper called inside an already
# session-guarded / item-guarded context. It summons NO menu entities, so it adds no
# new MP-isolation seam. Reads ec.bc_* / ec.bq_* only — writes nothing player-state.
# Run as @s = the player.

# === FRAME HEADER ===
tellraw @s ""
tellraw @s [{text:"═══════════════════════════════",color:"#C49A6C"}]
tellraw @s [{text:"  ⚒ ",color:"#E8C9A0"},{text:"The Handcrafter's Tome",color:"#C49A6C",bold:true},{text:" ⚒",color:"#E8C9A0"}]
tellraw @s [{text:"  The Maker's Path",color:"gray",italic:true}]
tellraw @s [{text:"═══════════════════════════════",color:"#C49A6C"}]
tellraw @s ""

# === 1. RANK LADDER (current rank highlighted) ===
# Card not yet seeded (no first-block / backfill choice) -> gentle prompt, skip ladder.
execute unless score @s ec.bc_seeded matches 1 run tellraw @s [{text:"  ✦ ",color:"yellow"},{text:"Your Handcrafter card has not yet opened.",color:"white"}]
execute unless score @s ec.bc_seeded matches 1 run tellraw @s [{text:"  Place your first block to begin the Maker's Path.",color:"gray"}]
execute unless score @s ec.bc_seeded matches 1 run tellraw @s ""
execute if score @s ec.bc_seeded matches 1 run function evercraft:handcrafter/book/render_ladder

# === 2. NEXT-RANK PROGRESS (ec.bc_credit toward the threshold need) ===
# bc/threshold writes #bc_need ec.temp = the credit required to reach (rank+1).
execute if score @s ec.bc_seeded matches 1 run function evercraft:guild_cards/bc/threshold
execute if score @s ec.bc_seeded matches 1 if score @s ec.bc_rank matches 12 run tellraw @s [{text:"  Maxed: ",color:"gray"},{text:"the Maker's Path is complete.",color:"#FFD27F",italic:true}]
execute if score @s ec.bc_seeded matches 1 unless score @s ec.bc_rank matches 12 run tellraw @s [{text:"  Toward next tier: ",color:"gray"},{score:{name:"@s",objective:"ec.bc_credit"},color:"#C49A6C",bold:true},{text:" / ",color:"dark_gray"},{score:{name:"#bc_need",objective:"ec.temp"},color:"#E8C9A0"},{text:" blocks",color:"gray"}]
# Eligible-to-claim hint (the village board is where the rank is actually claimed).
execute if score @s ec.bc_seeded matches 1 if score @s ec.bc_eligible matches 1 run tellraw @s [{text:"  ▸ ",color:"#7FD17F"},{text:"Eligible to advance! ",color:"#7FD17F",bold:true},{text:"Claim it at a crafting-village board.",color:"gray"}]
execute if score @s ec.bc_seeded matches 1 run tellraw @s ""

# === 3. UNLOCKED PERMANENT PERKS (rank-gated) ===
execute if score @s ec.bc_seeded matches 1 run tellraw @s [{text:"  Permanent Perks",color:"#E8C9A0",bold:true}]
execute if score @s ec.bc_seeded matches 1 unless score @s ec.bc_rank matches 7.. run tellraw @s [{text:"   None yet — reach rank A (7) for Builder's Reach.",color:"dark_gray",italic:true}]
execute if score @s ec.bc_rank matches 7.. run tellraw @s [{text:"   ⚒ ",color:"#C49A6C"},{text:"Builder's Reach",color:"#C49A6C"},{text:" — extended block-placement range",color:"gray"}]
execute if score @s ec.bc_rank matches 8.. run tellraw @s [{text:"   ⚒ ",color:"#C49A6C"},{text:"Resourceful Hands",color:"#C49A6C"},{text:" — a chance to refund placed blocks",color:"gray"}]
execute if score @s ec.bc_rank matches 9.. run tellraw @s [{text:"   ⚒ ",color:"#C49A6C"},{text:"Sure Footing",color:"#C49A6C"},{text:" — slow-fall while building",color:"gray"}]
execute if score @s ec.bc_rank matches 10.. run tellraw @s [{text:"   ⚒ ",color:"gold"},{text:"Tireless Mason",color:"gold"},{text:" — Haste while building",color:"gray"}]
execute if score @s ec.bc_seeded matches 1 run tellraw @s ""

# === 4. ACTIVE BUILD QUEST (reads ec.bq_* — the questline started on first block) ===
tellraw @s [{text:"  The Builder's Quests",color:"#E8C9A0",bold:true}]
# Not started: the line begins automatically on first block placed (on_first_place).
execute unless score @s ec.bq_active matches 1 run tellraw @s [{text:"   Your quest line awaits — place your first block.",color:"dark_gray",italic:true}]
# Completed the whole line.
execute if score @s ec.bq_complete matches 1 run tellraw @s [{text:"   ★ ",color:"gold"},{text:"Quests Complete — every stone set true.",color:"yellow"}]
# Active + not complete: show the current quest title + live progress (data-driven).
execute if score @s ec.bq_active matches 1 unless score @s ec.bq_complete matches 1 run function evercraft:build_quests/get_quest
execute if score @s ec.bq_active matches 1 unless score @s ec.bq_complete matches 1 run data modify storage evercraft:bq_cur o set from storage evercraft:bq_cur quest
execute if score @s ec.bq_active matches 1 unless score @s ec.bq_complete matches 1 run function evercraft:build_quests/overview_macro with storage evercraft:bq_cur o
execute if score @s ec.bq_active matches 1 unless score @s ec.bq_complete matches 1 run tellraw @s [{text:"    Progress: ",color:"gray"},{score:{name:"@s",objective:"ec.bq_prog"},color:"green",bold:true}]
# C0 lore line for the active beat (muted in Quick Mode) + [Skip ▸] button — the
# same affordance the Builder's-Path overview renders, so the Tome is self-sufficient
# without a separate (op-only) /function chat link. (Mirrors build_quests/overview.)
execute if score @s ec.bq_active matches 1 unless score @s ec.bq_complete matches 1 run data modify storage evercraft:qp_cur o set from storage evercraft:bq_cur quest
execute if score @s ec.bq_active matches 1 unless score @s ec.bq_complete matches 1 run function evercraft:quest_pacing/render_lore
# Pointer: the full Builder's Path log lives under Grand Quests (Codex → ✦ Grand Quests).
execute if score @s ec.bq_active matches 1 unless score @s ec.bq_complete matches 1 run tellraw @s [{text:"   Full quest log: ",color:"dark_gray"},{text:"Codex → ✦ Grand Quests → Build",color:"#9A8C7C",italic:true}]
tellraw @s ""

# === FRAME FOOTER ===
tellraw @s [{text:"═══════════════════════════════",color:"#C49A6C"}]
