#!/usr/bin/env ruby
# encoding: UTF-8
# Usage: hook-continue <index_path> <store_root> <mode>
# Renders the dashboard `continue` cockpit as UserPromptSubmit additionalContext.
# Exits silently (no output) if the dashboard produces nothing — the bash caller
# then falls back to a minimal notice.

require "json"
require "open3"
require_relative "lib/dashboard_banner"

index_path, _store_root, _mode = ARGV
exit 0 unless index_path && File.exist?(index_path)

dashboard = File.expand_path("dashboard.rb", __dir__)
exit 0 unless File.exist?(dashboard)

cockpit, _err, status = Open3.capture3({"RUBYOPT" => nil}, "ruby", dashboard, "continue")
exit 0 unless status.success? && !cockpit.strip.empty?

context = cockpit.rstrip +
  "\n\nFollow the plastic-continuing skill workflow to resume: " \
  "if active intents exist, read their state and resume; otherwise surface the " \
  "most-valuable next work and any stale or triage intents from the board above."

payload = {
  "hookSpecificOutput" => {
    "hookEventName" => "UserPromptSubmit",
    "additionalContext" => context
  }
}

# Hook-owned systemMessage floor (intent 125, Task 6): a hard fallback summary
# independent of the agent's reply, mirroring hook-session-start's BootBanner
# pattern. Best-effort only — any failure here (subprocess, JSON, renderer)
# degrades silently, omitting systemMessage; this hook must never crash
# UserPromptSubmit over a summary line.
begin
  data_json, _data_err, data_status = Open3.capture3({"RUBYOPT" => nil}, "ruby", dashboard, "continue", "--data")
  if data_status.success?
    banner = DashboardBanner.render(JSON.parse(data_json))
    payload["systemMessage"] = banner if banner
  end
rescue StandardError
  nil
end

puts JSON.generate(payload)
