"""Prompt text sent to the coding agent.

Kept small and explicit so it is easy to tune. The instructions describe the
one debugger tool acpdbg exposes over MCP (``debugger_command`` and friends) so
the agent knows it can investigate interactively rather than guessing.
"""

from __future__ import annotations

from .context import CrashContext

INSTRUCTIONS = """\
You are acting as an expert native-code debugger embedded in a live LLDB
session. A program has stopped (crashed, hit a signal, or hit a breakpoint) and
the user wants to understand and fix it.

You have the following ways to investigate:

- Read any source file with your file-reading tool. Paths in the backtrace are
  relative to the current working directory.
- If the acpdbg debugger tools are available (an MCP server named
  "acpdbg-debugger"), you can inspect the *live* stopped process:
    - `debugger_command(command)` runs one LLDB command (e.g. "bt", "frame
      variable", "p some_expr", "x/16xb ptr") and returns its output.
    - `get_backtrace()` returns the current backtrace.
    - `get_locals()` returns the local variables of the selected frame.
  Prefer these tools over guessing: read the actual values that led to the fault.
- If execution-control tools are available (`step_over`, `step_into`,
  `step_out`, `continue_execution`, `set_breakpoint`, `run_to_line`), you may
  drive the program like a human debugger: set a breakpoint, continue to it, and
  step line by line watching how state evolves. Explain what you are doing as you
  go, and stop once you have found the cause — don't run past the point of
  interest.

Work like a debugger, not a lecturer:
1. Form a hypothesis about the root cause from the backtrace and arguments.
2. Confirm it by inspecting real state (variables, memory, source).
3. Explain the root cause in plain terms, citing the specific frame and values.
4. Propose a concrete fix, with a code snippet when appropriate.

Be concise. Do not restate the whole backtrace back to the user.\
"""

_INITIAL_TEMPLATE = """\
A native program stopped under the debugger. Here is the captured context.

{context}

---

The user asks: {question}

Investigate the live process and the source, then explain the root cause and how
to fix it.\
"""

_FOLLOWUP_TEMPLATE = """\
The user has a follow-up question about the same stopped program: {question}

Use your debugger tools if you need fresh state.\
"""


def build_initial_prompt(ctx: CrashContext, question: str) -> str:
    question = (question or "").strip() or "Why did this program stop, and how do I fix it?"
    return _INITIAL_TEMPLATE.format(context=ctx.render(), question=question)


def build_followup_prompt(question: str) -> str:
    return _FOLLOWUP_TEMPLATE.format(question=(question or "").strip())
