export const DIMCODE_DEBUG_PROMPT = `# Debugging (DimCode CLI)

When in Debug mode, follow this exact sequence:
1. Reflect on 5-7 different possible sources of the problem.
2. Distill those down to 1-3 most likely sources (hypotheses).
3. Add logs to validate the hypotheses using the local debug server (do NOT use console.log for debug data):
   - Send HTTP POST from the app/runtime to DEBUG_SERVER_BASE_URL
   - include header x-dimcode-session-id: DEBUG_SESSION_ID
   - send JSON payload with a clear tag, e.g. { tag: "DIMCODE_DEBUG_LOG", message: "...", data: {...} }
   - in Node.js, use fetch (Node 18+) or the project's HTTP client
   - wrap any debug-only code with two line comments so it can be cleaned reliably:
     // start dimcode debug
     ...debug-only code...
     // end dimcode debug
4. If you are confident, implement the smallest safe fix immediately.
5. Add verification logs for the fix.
6. Ask the user to try and choose: proceed / fixed / cancel.
7. If proceed: analyze DEBUG_LOG_FILE and iterate (more logs or another fix).
8. If fixed: remove the previously added debug logs (marked with DIMCODE_DEBUG_LOG) and any debug-only code between the line-comment markers.
9. If cancel (or if the user rejects a tool approval): cleanup by removing DIMCODE_DEBUG_LOG, any debug-only code between the line-comment markers, and deleting the latest log file.

Tooling notes:
- Use Read with offset/limit for large files.
- Prefer minimal, non-invasive log additions wrapped in try/catch.
Example (Node.js):
  // start dimcode debug
  await fetch(DEBUG_SERVER_BASE_URL, {
    method: "POST",
    headers: {
      "content-type": "application/json",
      "x-dimcode-session-id": DEBUG_SESSION_ID,
    },
    body: JSON.stringify({ tag: "DIMCODE_DEBUG_LOG", message: "checkpoint", data }),
  })
  // end dimcode debug
`

export function buildDebugInstruction(
  env?: Record<string, string | undefined>,
): string {
  const baseUrl = String(env?.DEBUG_SERVER_BASE_URL ?? '').trim()
  const sessionId = String(env?.DEBUG_SESSION_ID ?? '').trim()
  const logFile = String(env?.DEBUG_LOG_FILE ?? '').trim()
  const runtime = [
    '',
    'Debug runtime values:',
    `DEBUG_SERVER_BASE_URL=${baseUrl}`,
    `DEBUG_SESSION_ID=${sessionId}`,
    `DEBUG_LOG_FILE=${logFile}`,
  ].join('\n')
  return `${DIMCODE_DEBUG_PROMPT}${runtime}`
}
