# ============================================================
# Chronicle Framework — Unified Overview digest (compute-on-open, ZERO aggregate)
# (Council #2 Wave C2.) The cross-chronicle "separable but tracked generally"
# digest. Reached from the Tier-1 [★ Overview] button (Adv.GqClick3 in
# codex/hub/grand_quests/check_clicks_chron). A clean tellraw page — the SAME
# shape as the per-line overviews (build_quests/overview, cooking/questline/
# overview) and stats/profile/show. Run as @s = the player.
#
# CONTRACT (locked):
#   • ZERO new objectives, ZERO storage (C5 forbids an aggregate; the wave plan
#     mandates "no aggregate scoreboard — zero new writer, zero tick cost").
#   • Computes LIVE on open by reading each built line's existing PUBLIC pointers
#     (ec.<line>_active / ec.<line>_complete) — mirrors profile/refresh_snapshot,
#     which rebuilds the whole card from live scoreboards per render. NO write to
#     any line's state (read-only; the one-writer law is untouched).
#   • Per-line glyph: ✓ complete (active & ec.<line>_complete) / ◔ in progress
#     (active, not complete) / ✗ not started (inactive).
#   • The "Both" header counts STARTED (X/N) + COMPLETE (Y/N) over the BUILT
#     lines only. Tinkering, when it ships, is counted ONCE here though rendered
#     in both Tier-2 menus (S12) — it lives in the Crafter tally below, never
#     double-added to the Adventurer one.
#   • A single-chronicle player (lens 1 or 2) sees THEIR block expanded and the
#     OTHER collapsed to a clickable "⊕ … tap to begin" opt-in that calls
#     chronicle/set_view (the lens mutator). Lens 3 (Both) expands both.
#   • The tracked line shows a ★ — DEFERRED behind round-1's set_track: ec.gq_track
#     is NOT yet declared (round-1 grand_quests/set_track is unbuilt), and reading
#     an undeclared objective in `execute if score` faults at runtime. So this file
#     references ec.gq_track on ZERO executable lines (mirrors C1, which left it
#     read-only and unreferenced). The ★ render slot lights up automatically once
#     set_track declares ec.gq_track — see the EXTENSION CONTRACT at the bottom.
#
# WHICH LINES ARE BUILT (the only pointers this file may read — extend per line):
#   Crafter:    Build (ec.bq_*) · Fishing (ec.fq_*) · Cooking (ec.cq_*)
#   Adventurer: none yet (C5 ec.ac_* / C6 ec.ak_* / C7 ec.tnk_* — honest notice)
# ============================================================

# ─────────────────────────────────────────────────────────────────────────────
# TALLY (compute-on-open). #chron_n = built-line count; #chron_started /
# #chron_complete accumulate over the BUILT lines. All on ec.temp (scratch),
# reset at the top so a stale value never leaks across opens. Serial @s execution
# is the per-player fence (same as profile/refresh_snapshot's global scratch).
# ─────────────────────────────────────────────────────────────────────────────
scoreboard players set #chron_n ec.temp 0
scoreboard players set #chron_started ec.temp 0
scoreboard players set #chron_complete ec.temp 0

# --- Build (ec.bq_*) ---
scoreboard players add #chron_n ec.temp 1
execute if score @s ec.bq_active matches 1.. run scoreboard players add #chron_started ec.temp 1
execute if score @s ec.bq_complete matches 1 run scoreboard players add #chron_complete ec.temp 1
# --- Fishing (ec.fq_*) ---
scoreboard players add #chron_n ec.temp 1
execute if score @s ec.fq_active matches 1.. run scoreboard players add #chron_started ec.temp 1
execute if score @s ec.fq_complete matches 1 run scoreboard players add #chron_complete ec.temp 1
# --- Cooking (ec.cq_*) ---
scoreboard players add #chron_n ec.temp 1
execute if score @s ec.cq_active matches 1.. run scoreboard players add #chron_started ec.temp 1
execute if score @s ec.cq_complete matches 1 run scoreboard players add #chron_complete ec.temp 1
# --- Tinkering (ec.tnk_*) — UNIVERSAL line, counted ONCE here (S12), even though it renders
#     in BOTH the Crafter block below AND the Adventurer block. Its tally trio lives ONLY here
#     (the Adventurer-block row is a read-only echo with NO second trio). ---
scoreboard players add #chron_n ec.temp 1
execute if score @s ec.tnk_active matches 1.. run scoreboard players add #chron_started ec.temp 1
execute if score @s ec.tnk_complete matches 1 run scoreboard players add #chron_complete ec.temp 1
# --- Grand Chronicle (ec.gch_*) — the UNIVERSAL convergence line (W2 2026-06-10), counted ONCE
#     here per the S12 rule; rendered in BOTH blocks below (Crafter row + Adventurer echo). ---
scoreboard players add #chron_n ec.temp 1
execute if score @s ec.gch_active matches 1.. run scoreboard players add #chron_started ec.temp 1
execute if score @s ec.gch_complete matches 1 run scoreboard players add #chron_complete ec.temp 1

# Mirror the tallies into ec.var so a {score:...} component can render them with a
# fixed fake-player handle (tellraw can't read a #handle on ec.temp directly with
# the same name twice in one page without re-reading; using ec.var keeps it clean).
scoreboard players operation #ch_n ec.var = #chron_n ec.temp
scoreboard players operation #ch_started ec.var = #chron_started ec.temp
scoreboard players operation #ch_complete ec.var = #chron_complete ec.temp

# ─────────────────────────────────────────────────────────────────────────────
# FRAME HEADER
# ─────────────────────────────────────────────────────────────────────────────
tellraw @s ""
tellraw @s [{text:"═══════════════════════════════",color:"gold"}]
tellraw @s [{text:"  ★ ",color:"gold"},{text:"Unified Overview",color:"gold",bold:true},{text:" ★",color:"gold"}]
tellraw @s [{text:"  Your progress across both chronicles",color:"gray",italic:true}]
tellraw @s [{text:"═══════════════════════════════",color:"gold"}]
tellraw @s ""

# ─────────────────────────────────────────────────────────────────────────────
# ★ TRACKED PATH (extension-contract item 2 — LIVE since W1 2026-06-10:
# grand_quests/set_track declares + solely writes ec.gq_track; slots per
# quest_pacing/do_skip). One named row for whichever line is pinned.
# ─────────────────────────────────────────────────────────────────────────────
execute if score @s ec.gq_track matches 1 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Fisher's Path",color:"aqua",bold:true}]
execute if score @s ec.gq_track matches 4 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Cook's Path",color:"green",bold:true}]
execute if score @s ec.gq_track matches 5 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Smith's Path",color:"green",bold:true}]
execute if score @s ec.gq_track matches 6 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Chemist's Path",color:"green",bold:true}]
execute if score @s ec.gq_track matches 7 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"Build & Conquer",color:"red",bold:true}]
execute if score @s ec.gq_track matches 8 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Build Quests",color:"green",bold:true}]
execute if score @s ec.gq_track matches 9 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Awakening",color:"red",bold:true}]
execute if score @s ec.gq_track matches 10 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Tinkerer's Path",color:"light_purple",bold:true}]
execute if score @s ec.gq_track matches 11 run tellraw @s [{text:"  ★ Tracked: ",color:"gold"},{text:"The Grand Chronicle",color:"gold",bold:true}]

# Grand Chronicle begin row (W2; the ✓/◔/✗ status rows live in BOTH lens blocks below —
# QR-P2 verdict 2026-06-10 — so only the entry affordance renders here).
function evercraft:grand_quests/unlock_check
execute unless score @s ec.gch_active matches 1.. if score #gch_unlocked ec.temp matches 1 run tellraw @s [{text:"  ",color:"gray"},{text:"[✦ Begin the Grand Chronicle →]",color:"#FFD479",bold:true,click_event:{action:"run_command",command:"/trigger ec.gq_trk set 60"},hover_event:{action:"show_text",value:{text:"Both chronicles are underway — walk the seam between them.",color:"gray"}}}]

# ─────────────────────────────────────────────────────────────────────────────
# "BOTH" HEADER — X/N started + Y/N complete (the aggregate, computed live)
# ─────────────────────────────────────────────────────────────────────────────
tellraw @s [{text:"  ⚑ ",color:"gold"},{text:"Both Chronicles",color:"gold",bold:true}]
tellraw @s [{text:"     Lines started:  ",color:"gray"},{score:{name:"#ch_started",objective:"ec.var"},color:"green",bold:true},{text:" / ",color:"dark_gray"},{score:{name:"#ch_n",objective:"ec.var"},color:"white"}]
tellraw @s [{text:"     Lines complete: ",color:"gray"},{score:{name:"#ch_complete",objective:"ec.var"},color:"gold",bold:true},{text:" / ",color:"dark_gray"},{score:{name:"#ch_n",objective:"ec.var"},color:"white"}]
tellraw @s ""

# ─────────────────────────────────────────────────────────────────────────────
# CRAFTER BLOCK
#   Expanded when lens = 2 (Crafter) or 3 (Both) — and ALSO when lens is view-less
#   / out-of-range (the digest never hides a built line for an unknown lens). It
#   collapses to an opt-in ONLY for a dedicated Adventurer player (lens 1).
# ─────────────────────────────────────────────────────────────────────────────

# --- Expanded header (lens != 1) ---
execute unless score @s ec.chron_view matches 1 run tellraw @s [{text:"  ⚒ ",color:"green"},{text:"Crafter Chronicle",color:"green",bold:true}]

# Build row
execute unless score @s ec.chron_view matches 1 if score @s ec.bq_active matches 1.. if score @s ec.bq_complete matches 1 run tellraw @s [{text:"     ✓ ",color:"green",bold:true},{text:"The Builder's Path",color:"white"},{text:"  — complete",color:"dark_gray"}]
execute unless score @s ec.chron_view matches 1 if score @s ec.bq_active matches 1.. unless score @s ec.bq_complete matches 1 run tellraw @s [{text:"     ◔ ",color:"gold",bold:true},{text:"The Builder's Path",color:"white"},{text:"  — in progress",color:"dark_gray"}]
execute unless score @s ec.chron_view matches 1 unless score @s ec.bq_active matches 1.. run tellraw @s [{text:"     ✗ ",color:"dark_gray",bold:true},{text:"The Builder's Path",color:"gray"},{text:"  — not begun",color:"dark_gray"}]

# Fishing row
execute unless score @s ec.chron_view matches 1 if score @s ec.fq_active matches 1.. if score @s ec.fq_complete matches 1 run tellraw @s [{text:"     ✓ ",color:"green",bold:true},{text:"The Fisher's Path",color:"white"},{text:"  — complete",color:"dark_gray"}]
execute unless score @s ec.chron_view matches 1 if score @s ec.fq_active matches 1.. unless score @s ec.fq_complete matches 1 run tellraw @s [{text:"     ◔ ",color:"gold",bold:true},{text:"The Fisher's Path",color:"white"},{text:"  — in progress",color:"dark_gray"}]
execute unless score @s ec.chron_view matches 1 unless score @s ec.fq_active matches 1.. run tellraw @s [{text:"     ✗ ",color:"dark_gray",bold:true},{text:"The Fisher's Path",color:"gray"},{text:"  — not begun",color:"dark_gray"}]

# Cooking row
execute unless score @s ec.chron_view matches 1 if score @s ec.cq_active matches 1.. if score @s ec.cq_complete matches 1 run tellraw @s [{text:"     ✓ ",color:"green",bold:true},{text:"The Cook's Path",color:"white"},{text:"  — complete",color:"dark_gray"}]
execute unless score @s ec.chron_view matches 1 if score @s ec.cq_active matches 1.. unless score @s ec.cq_complete matches 1 run tellraw @s [{text:"     ◔ ",color:"gold",bold:true},{text:"The Cook's Path",color:"white"},{text:"  — in progress",color:"dark_gray"}]
execute unless score @s ec.chron_view matches 1 unless score @s ec.cq_active matches 1.. run tellraw @s [{text:"     ✗ ",color:"dark_gray",bold:true},{text:"The Cook's Path",color:"gray"},{text:"  — not begun",color:"dark_gray"}]

# Tinkering row (UNIVERSAL — its tally is counted ONCE above, in the Crafter block; the
# Adventurer block renders a read-only ECHO of this same ec.tnk_* state with NO second tally,
# S12). "(universal)" marks it as shown in both chronicles.
execute unless score @s ec.chron_view matches 1 if score @s ec.tnk_active matches 1.. if score @s ec.tnk_complete matches 1 run tellraw @s [{text:"     ✓ ",color:"green",bold:true},{text:"The Tinkerer's Path",color:"white"},{text:"  — complete",color:"dark_gray"},{text:"  (universal)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 1 if score @s ec.tnk_active matches 1.. unless score @s ec.tnk_complete matches 1 run tellraw @s [{text:"     ◔ ",color:"gold",bold:true},{text:"The Tinkerer's Path",color:"white"},{text:"  — in progress",color:"dark_gray"},{text:"  (universal)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 1 unless score @s ec.tnk_active matches 1.. run tellraw @s [{text:"     ✗ ",color:"dark_gray",bold:true},{text:"The Tinkerer's Path",color:"gray"},{text:"  — not begun",color:"dark_gray"},{text:"  (universal)",color:"dark_gray",italic:true}]

# Grand Chronicle row (UNIVERSAL convergence line — tally counted ONCE above; the Adventurer
# block renders a read-only echo, S12 pattern; QR-P2 verdict 2026-06-10).
execute unless score @s ec.chron_view matches 1 if score @s ec.gch_active matches 1.. if score @s ec.gch_complete matches 1 run tellraw @s [{text:"     ✓ ",color:"green",bold:true},{text:"The Grand Chronicle",color:"#FFD479"},{text:"  — the seam holds",color:"dark_gray"},{text:"  (convergence)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 1 if score @s ec.gch_active matches 1.. unless score @s ec.gch_complete matches 1 run tellraw @s [{text:"     ◔ ",color:"gold",bold:true},{text:"The Grand Chronicle",color:"#FFD479"},{text:"  — beat ",color:"dark_gray"},{score:{name:"@s",objective:"ec.gch_quest"},color:"white"},{text:"/16",color:"dark_gray"},{text:"  (convergence)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 1 unless score @s ec.gch_active matches 1.. run tellraw @s [{text:"     ✗ ",color:"dark_gray",bold:true},{text:"The Grand Chronicle",color:"gray"},{text:"  — not begun",color:"dark_gray"},{text:"  (convergence)",color:"dark_gray",italic:true}]

# --- Collapsed opt-in (lens = 1, dedicated Adventurer) ---
# A single clickable line that opens the Crafter chronicle in the hub. It calls
# chronicle/set_view (the lens mutator — same target the Settings Chronicle-Lens
# row uses). The link is the law-compliant clickable-/function affordance already
# used by stats/profile/show ([Share] → /function evercraft:stats/profile/share);
# no /trigger discoverability text, no new objective.
execute if score @s ec.chron_view matches 1 run tellraw @s [{text:"  ⊕ ",color:"green"},{text:"Crafter Chronicle",color:"green",bold:true},{text:" — ",color:"dark_gray"},{text:"[tap to view]",color:"#7FB3FF",bold:true,click_event:{action:"run_command",command:"/function evercraft:chronicle/set_view"},hover_event:{action:"show_text",value:{text:"Bring the Crafter lines into your Grand Quests lens. Changes only what you see — your progress in every line is untouched.",color:"gray"}}}]
tellraw @s ""

# ─────────────────────────────────────────────────────────────────────────────
# ADVENTURER BLOCK
#   Expanded when lens = 1 (Adventurer) or 3 (Both) or view-less/out-of-range.
#   Collapses to an opt-in ONLY for a dedicated Crafter player (lens 2).
#   No Adventurer line is BUILT yet (C5/C6/C7) — the expanded form is an honest
#   "arriving soon" notice that reads NO unbuilt ec.ac_*/ec.ak_*/ec.tnk_* pointer.
# ─────────────────────────────────────────────────────────────────────────────

# --- Expanded header + honest notice (lens != 2) ---
execute unless score @s ec.chron_view matches 2 run tellraw @s [{text:"  ⚔ ",color:"red"},{text:"Adventurer Chronicle",color:"red",bold:true}]
execute unless score @s ec.chron_view matches 2 run tellraw @s [{text:"     The Awakening • Build & Conquer",color:"gray"}]
execute unless score @s ec.chron_view matches 2 run tellraw @s [{text:"     — open these from the Adventurer Tier-2 menu —",color:"dark_gray",italic:true}]
# Tinkering read-only ECHO (S12): the UNIVERSAL line renders here too, but its tally is
# counted ONCE (in the Crafter block above) — this row reads the SAME ec.tnk_* state with NO
# second tally trio. So a Both/Adventurer player sees Tinkering in both chronicles, counted once.
execute unless score @s ec.chron_view matches 2 if score @s ec.tnk_active matches 1.. if score @s ec.tnk_complete matches 1 run tellraw @s [{text:"     ✓ ",color:"green",bold:true},{text:"The Tinkerer's Path",color:"white"},{text:"  — complete",color:"dark_gray"},{text:"  (universal)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 2 if score @s ec.tnk_active matches 1.. unless score @s ec.tnk_complete matches 1 run tellraw @s [{text:"     ◔ ",color:"gold",bold:true},{text:"The Tinkerer's Path",color:"white"},{text:"  — in progress",color:"dark_gray"},{text:"  (universal)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 2 unless score @s ec.tnk_active matches 1.. run tellraw @s [{text:"     ✗ ",color:"dark_gray",bold:true},{text:"The Tinkerer's Path",color:"gray"},{text:"  — not begun",color:"dark_gray"},{text:"  (universal)",color:"dark_gray",italic:true}]
# Grand Chronicle read-only ECHO (convergence line; tally counted ONCE in the Crafter block).
execute unless score @s ec.chron_view matches 2 if score @s ec.gch_active matches 1.. if score @s ec.gch_complete matches 1 run tellraw @s [{text:"     ✓ ",color:"green",bold:true},{text:"The Grand Chronicle",color:"#FFD479"},{text:"  — the seam holds",color:"dark_gray"},{text:"  (convergence)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 2 if score @s ec.gch_active matches 1.. unless score @s ec.gch_complete matches 1 run tellraw @s [{text:"     ◔ ",color:"gold",bold:true},{text:"The Grand Chronicle",color:"#FFD479"},{text:"  — beat ",color:"dark_gray"},{score:{name:"@s",objective:"ec.gch_quest"},color:"white"},{text:"/16",color:"dark_gray"},{text:"  (convergence)",color:"dark_gray",italic:true}]
execute unless score @s ec.chron_view matches 2 unless score @s ec.gch_active matches 1.. run tellraw @s [{text:"     ✗ ",color:"dark_gray",bold:true},{text:"The Grand Chronicle",color:"gray"},{text:"  — not begun",color:"dark_gray"},{text:"  (convergence)",color:"dark_gray",italic:true}]

# --- Collapsed opt-in (lens = 2, dedicated Crafter) ---
execute if score @s ec.chron_view matches 2 run tellraw @s [{text:"  ⊕ ",color:"red"},{text:"Adventurer Chronicle",color:"red",bold:true},{text:" — ",color:"dark_gray"},{text:"[tap to view]",color:"#7FB3FF",bold:true,click_event:{action:"run_command",command:"/function evercraft:chronicle/set_view"},hover_event:{action:"show_text",value:{text:"Bring the Adventurer lines into your Grand Quests lens. Changes only what you see — your progress in every line is untouched.",color:"gray"}}}]
tellraw @s ""

# ─────────────────────────────────────────────────────────────────────────────
# HOUSING CONVERGENCE CHECKLIST (W6-housing) — the read-only ✓/✗ feature card.
# Compute-on-open, ZERO new objectives (pure reads of existing housing state), each
# ✗ links to the Builder's Path overview (where its teaching chapter H1..H10 lives).
# Rendered for every lens (it is a Crafter-chronicle housing feature; the digest never
# hides a built checklist). Additive — does not touch the tally or the blocks above.
# ─────────────────────────────────────────────────────────────────────────────
function evercraft:codex/hub/grand_quests/digest_features

# ─────────────────────────────────────────────────────────────────────────────
# FOOTER + a confirm sound (mirrors the line overviews' open feedback)
# ─────────────────────────────────────────────────────────────────────────────
tellraw @s [{text:"  Switching lens loses ZERO progress — every line keeps its own pointer.",color:"dark_gray",italic:true}]
tellraw @s [{text:"  ",color:"gray"},{text:"[★ Track a Path]",color:"#FFD479",bold:true,click_event:{action:"run_command",command:"/trigger ec.gq_trk set 50"},hover_event:{action:"show_text",value:{text:"Pin a line — [Skip ▸] and the Chronicle star follow it.",color:"gray"}}}]
tellraw @s [{text:"═══════════════════════════════",color:"gold"}]
execute if score @s ec.fx_snd matches 1.. run playsound minecraft:item.book.page_turn master @s ~ ~ ~ 0.7 1.0

# ─────────────────────────────────────────────────────────────────────────────
# EXTENSION CONTRACT (each future wave extends THIS file, additively):
#   1. NEW BUILT LINE: in the TALLY block add the 3-line trio
#        scoreboard players add #chron_n ec.temp 1
#        execute if score @s ec.<line>_active matches 1.. run scoreboard players add #chron_started ec.temp 1
#        execute if score @s ec.<line>_complete matches 1 run scoreboard players add #chron_complete ec.temp 1
#      then add a ✓/◔/✗ row in the correct block (Crafter or Adventurer), gated by
#      the same `unless ec.chron_view matches <opposite>` collapse guard. TINKERING
#      (ec.tnk_*) is tallied ONCE — put its trio + row in the CRAFTER block only,
#      and render a SECOND read-only ✓/◔/✗ row in the Adventurer block WITHOUT a
#      second tally trio (S12: rendered in both tiers, counted once).
#   2. ★ TRACKED-LINE STAR (deferred): once round-1 grand_quests/set_track declares
#      ec.gq_track, prepend a star to the tracked line's row, e.g.
#        execute if score @s ec.gq_track matches 8 if score @s ec.bq_active matches 1.. run tellraw @s [{text:"  ★ tracked",color:"gold",italic:true}]
#      (gq_track slots: Build=8, Cooking=4, … per quest_pacing/do_skip). NOTHING in
#      this file may read ec.gq_track until that objective is declared.
# ─────────────────────────────────────────────────────────────────────────────
