#!/usr/bin/env python
"""PRD Plugin nudge hook for Claude Code.

Wired to SessionStart and UserPromptSubmit in .claude/settings.json. Its stdout
is injected into the model's context, so the routing reminder is always present
(the always-on pressure Codex gets from re-reading AGENTS.md). It does not — and
cannot — force a skill to run; it makes ignoring the workflow much harder.
Hard guarantees live in the commit/CI gate (prd_gate.py), not here.
"""

print(
    "[PRD Plugin] This repo uses the PRD Plugin method. Before acting:\n"
    "- Consult the project-decision-policy skill to pick the right workflow and "
    "apply the configured autonomy tier.\n"
    "- Capture new/changed work as REQ-*/TRK-*; route a mid-development "
    "add/change/remove/defer through project-change-request (don't edit "
    "PRD/architecture/plan ad hoc).\n"
    "- Write a failing test first for code (project-test-driven-implementation); "
    "verify with fresh evidence (EV-*) before claiming done "
    "(project-verification-before-completion).\n"
    "- Query the wiki (project-llm-wiki) before re-deriving a known fact, and "
    "ingest durable domain/codebase knowledge into it as work closes.\n"
    "- Do not invent IDs/records or give time estimates. Update tracking/changelog "
    "silently as part of the work; never tell the user to wrap up, hand off, or "
    "close the session — session length and context pressure are not reasons to "
    "stop. Keep working until the task is done or the user stops you (compaction "
    "handles context automatically)."
)

# Surface a newer PRD Plugin version if one is known (cache-only — no network
# here; the cache is refreshed on Stop by prd_session_report.py). REQ-068.
import os


def _surface_version_update():
    try:
        for rel in (os.path.join(".prd_plugin", "scripts"), "scripts"):
            if os.path.isfile(os.path.join(rel, "prd_version_check.py")):
                import sys as _s
                _s.path.insert(0, rel)
                import prd_version_check as _vc
                # Refresh in-session (TTL-gated, short timeout, fail-open) so a
                # newly published version surfaces now, not a session later.
                s = _vc.check(".", fetch=lambda pkg: _vc.fetch_latest(pkg, timeout=4))
                if s.get("update_available"):
                    print(f"[PRD Plugin] update available: {s['latest']} (you're on "
                          f"{s['installed']}). Run: npm update prd-plugin && "
                          "npx prd-install . --force")
                return
    except Exception:
        pass  # never let the version check break the nudge


_surface_version_update()


def _surface_fork_update():
    # Surface a newer fork version when this repo is a configured fork consumer.
    # Inert unless fork.version_check is enabled with a source_url and a local
    # marker; TTL-cached and fail-open. REQ-081.
    try:
        for rel in (os.path.join(".prd_plugin", "scripts"), "scripts"):
            if os.path.isfile(os.path.join(rel, "fork_version_check.py")):
                import sys as _s
                _s.path.insert(0, rel)
                import fork_version_check as _fvc
                s = _fvc.check(".")
                if s.get("update_available"):
                    print(f"[PRD Plugin] a new fork version is available: "
                          f"{s['latest']} (you're on {s['current']}).")
                return
    except Exception:
        pass  # never let the fork check break the nudge


_surface_fork_update()

if os.path.isfile(
    os.path.join(".prd_plugin", "local", "wiki-backfill-needed")
) and not os.path.isfile(os.path.join("wiki", "index.md")):
    print(
        "[PRD Plugin] This repo just gained the LLM wiki but has none yet. Run a "
        "deep backfill: `python .prd_plugin/scripts/prd_wiki_backfill.py --plan`, "
        "compile it via the project-llm-wiki skill's Backfill mode, then delete "
        ".prd_plugin/local/wiki-backfill-needed."
    )
