#!/usr/bin/env bash
# nerf-report -- write a structured report file for the maintainer to triage.
# Generated by nerftools. Do not edit directly.

set -euo pipefail

# Stamped at plugin build time.
NERFTOOLS_VERSION="2.2.0"

# Reports live under $HOME; fail fast with an actionable message if it's
# unset rather than relying on `set -u` to crash later with "unbound variable".
: "${HOME:?nerf-report: HOME is not set; cannot determine reports directory}"

_usage() {
    cat >&2 <<EOF
Usage: nerf-report <kind> <tool> <body>

  <kind>  one of: bug, bypass, complaint, request
  <tool>  the nerf tool you're reporting about (e.g. nerf-az-repos-pr-edit),
          or "nerftools" for the package itself
  <body>  free-form prose describing what happened or what you want

Writes a Markdown report to ~/.nerftools/reports/ with auto-captured
context (timestamp, cwd, session ID, nerftools version).

Quote the body so it reaches the script as a single argument unprocessed
by the shell.
EOF
    exit 2
}

if [[ $# -ne 3 ]]; then
    _usage
fi

KIND="$1"
TOOL="$2"
BODY="$3"

case "$KIND" in
    bug | bypass | complaint | request) ;;
    *)
        echo "error: nerf-report: invalid kind '${KIND}'. Must be one of: bug, bypass, complaint, request" >&2
        exit 2
        ;;
esac

# Sanitize TOOL for use in a filename (anything outside the safe set
# becomes _). Reject empty after sanitizing.
SANITIZED_TOOL="${TOOL//[^A-Za-z0-9._-]/_}"
if [[ -z "$SANITIZED_TOOL" ]]; then
    echo "error: nerf-report: <tool> is empty after sanitization" >&2
    exit 2
fi

REPORTS_DIR="${HOME}/.nerftools/reports"
# Reports may contain sensitive context (cwd, session ID, agent-written
# body). Restrict permissions so other users on a shared machine cannot
# read them. umask makes new files 0600 and new directories 0700; the
# explicit chmod tightens an existing reports/ dir that may have been
# created with a looser umask by an older script. If we can't tighten,
# refuse to write rather than leaking sensitive context.
umask 077
mkdir -p "$REPORTS_DIR"
if ! chmod 0700 "$REPORTS_DIR"; then
    echo "error: nerf-report: could not restrict permissions on ${REPORTS_DIR}; refusing to write report" >&2
    exit 1
fi

TIMESTAMP_COMPACT="$(date -u +%Y%m%dT%H%M%SZ)"
TIMESTAMP_ISO="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
SESSION="${NERF_REPORT_SESSION:-${CLAUDE_SESSION_ID:-${CODEX_SESSION_ID:-unknown}}}"
CWD="$(pwd)"

# Deterministic filename: timestamp + kind + tool + version. Two reports
# in the same second land in the same file via `>>` -- frontmatter and
# all -- which is fine: the maintainer sees both entries together. No
# randomness means no collision-by-bad-luck concerns and no TOCTOU on
# create.
FILENAME="${TIMESTAMP_COMPACT}_${KIND}_${SANITIZED_TOOL}_${NERFTOOLS_VERSION}.md"
DEST="${REPORTS_DIR}/${FILENAME}"

# YAML-safe double-quoted string escaping for the frontmatter fields.
# Order matters: escape backslashes first, then quotes, then control chars,
# so the later substitutions don't double-escape the backslashes we just
# inserted.
_yaml_escape() {
    local s="$1"
    s="${s//\\/\\\\}"
    s="${s//\"/\\\"}"
    s="${s//$'\n'/\\n}"
    s="${s//$'\r'/\\r}"
    s="${s//$'\t'/\\t}"
    printf '%s' "$s"
}

{
    printf -- '---\n'
    printf 'kind: %s\n' "$KIND"
    printf 'tool: "%s"\n' "$(_yaml_escape "$TOOL")"
    printf 'nerftools_version: "%s"\n' "$NERFTOOLS_VERSION"
    printf 'session: "%s"\n' "$(_yaml_escape "$SESSION")"
    printf 'cwd: "%s"\n' "$(_yaml_escape "$CWD")"
    printf 'timestamp: "%s"\n' "$TIMESTAMP_ISO"
    printf -- '---\n\n'
    printf '%s\n' "$BODY"
} >> "$DEST"

echo "report written: $DEST"
