#!/usr/bin/env bash
set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
tmp_dir="$(mktemp -d)"
cleanup() {
  rm -rf "$tmp_dir"
}
trap cleanup EXIT

sed '$d' "${repo_dir}/templates/scripts/afk-workflow" >"${tmp_dir}/afk-workflow-lib"
sed '$d' "${repo_dir}/templates/scripts/review-pr" >"${tmp_dir}/review-pr-lib"

# shellcheck disable=SC1090
source "${tmp_dir}/afk-workflow-lib"
# shellcheck disable=SC1090
source "${tmp_dir}/review-pr-lib"

assert_grep() {
  local pattern="$1"
  local file="$2"
  local message="$3"

  if ! grep -q -- "$pattern" "$file"; then
    echo "$message" >&2
    echo "--- output ---" >&2
    cat "$file" >&2
    exit 1
  fi
}

assert_not_grep() {
  local pattern="$1"
  local file="$2"
  local message="$3"

  if grep -q -- "$pattern" "$file"; then
    echo "$message" >&2
    echo "--- output ---" >&2
    cat "$file" >&2
    exit 1
  fi
}

prompt_repo="${tmp_dir}/prompt-repo"
mkdir -p "${prompt_repo}/templates/docs/agents"
cp "${repo_dir}/templates/docs/agents/pr-review.md" "${prompt_repo}/templates/docs/agents/pr-review.md"
cp "${repo_dir}/templates/docs/agents/github-pr-reviewer.md" "${prompt_repo}/templates/docs/agents/github-pr-reviewer.md"

(
  cd "$prompt_repo"
  review_prompt 9 "Test PR" "https://github.com/acme/repo/pull/9" 0 >"${tmp_dir}/review-human.prompt"
  review_prompt 9 "Test PR" "https://github.com/acme/repo/pull/9" 1 >"${tmp_dir}/review-machine.prompt"
)

assert_grep "Machine-readable contract source: templates/docs/agents/github-pr-reviewer.md" "${tmp_dir}/review-machine.prompt" "machine-readable review prompt did not mention github-pr-reviewer contract source"
assert_grep "BEGIN_REVIEW_FIX_ISSUES_JSON" "${tmp_dir}/review-machine.prompt" "machine-readable review prompt did not include JSON markers"
assert_grep "memory_suggestions" "${tmp_dir}/review-machine.prompt" "machine-readable review prompt did not include memory_suggestions contract"
assert_not_grep "Use the machine-readable GitHub PR reviewer contract" "${tmp_dir}/review-human.prompt" "normal review prompt unexpectedly included machine-readable contract"

CREATED_ISSUES=()

gh() {
  if [[ "$1" == "issue" && "$2" == "create" ]]; then
    local title=""
    local body=""
    local labels=()

    shift 2
    while [[ $# -gt 0 ]]; do
      case "$1" in
        --title)
          title="$2"
          shift 2
          ;;
        --body)
          body="$2"
          shift 2
          ;;
        --label)
          labels+=("$2")
          shift 2
          ;;
        *)
          echo "unexpected gh issue create arg: $1" >&2
          return 1
          ;;
      esac
    done

    CREATED_ISSUES+=("${title}"$'\t'"${body}"$'\t'"$(IFS=,; echo "${labels[*]}")")
    return 0
  fi

  echo "unexpected gh invocation: $*" >&2
  return 1
}

review_file="${tmp_dir}/review.md"
cat >"$review_file" <<'REVIEW'
Findings omitted for test.

BEGIN_REVIEW_FIX_ISSUES_JSON
{
  "fix_issues": [
    {
      "title": "Missing verification for AC2",
      "severity": "P1",
      "file": "README.md",
      "body": "AC2 needs concrete verification evidence."
    }
  ],
  "memory_suggestions": [
    {
      "kind": "failure-pattern",
      "title": "Do not claim ACs without evidence",
      "target_file": "docs/memory/failure-patterns.md",
      "source": "PR #9 review finding: Missing verification for AC2",
      "body": "Future PRs must map each acceptance criterion to implementation and verification evidence."
    }
  ]
}
END_REVIEW_FIX_ISSUES_JSON
REVIEW

create_review_fix_issues 7 9 "$review_file" "afk"
create_memory_suggestion_issues 7 9 "$review_file" "afk"

missing_contract_review="${tmp_dir}/missing-contract-review.md"
cat >"$missing_contract_review" <<'REVIEW'
Findings: none.
REVIEW

if (validate_machine_readable_review_output "$missing_contract_review") >"${tmp_dir}/missing-contract.out" 2>&1; then
  echo "machine-readable review validation accepted output without JSON markers" >&2
  exit 1
fi
assert_grep "missing BEGIN_REVIEW_FIX_ISSUES_JSON block" "${tmp_dir}/missing-contract.out" "missing contract validation did not explain missing markers"

malformed_contract_review="${tmp_dir}/malformed-contract-review.md"
cat >"$malformed_contract_review" <<'REVIEW'
Findings: none.

BEGIN_REVIEW_FIX_ISSUES_JSON
{
  "fix_issues": []
}
END_REVIEW_FIX_ISSUES_JSON
REVIEW

if (validate_machine_readable_review_output "$malformed_contract_review") >"${tmp_dir}/malformed-contract.out" 2>&1; then
  echo "machine-readable review validation accepted JSON without memory_suggestions" >&2
  exit 1
fi
assert_grep "must include fix_issues and memory_suggestions arrays" "${tmp_dir}/malformed-contract.out" "malformed contract validation did not explain required arrays"

validate_machine_readable_review_output "$review_file"

if [[ "${#CREATED_ISSUES[@]}" -ne 2 ]]; then
  echo "expected two created issues, got ${#CREATED_ISSUES[@]}" >&2
  exit 1
fi

if [[ "${CREATED_ISSUES[0]}" != *"[review-fix] PR #9: Missing verification for AC2"* ]]; then
  echo "review-fix issue title was not created from fix_issues JSON" >&2
  exit 1
fi

if [[ "${CREATED_ISSUES[0]}" != *"review-fix,afk,ready-for-agent"* ]]; then
  echo "review-fix issue labels are wrong" >&2
  exit 1
fi

if [[ "${CREATED_ISSUES[1]}" != *"[memory-suggestion] PR #9: Do not claim ACs without evidence"* ]]; then
  echo "memory-suggestion issue title was not created from memory_suggestions JSON" >&2
  exit 1
fi

if [[ "${CREATED_ISSUES[1]}" != *"memory-suggestion,afk,ready-for-agent"* ]]; then
  echo "memory-suggestion issue labels are wrong" >&2
  exit 1
fi

if [[ "${CREATED_ISSUES[1]}" != *"Target file: docs/memory/failure-patterns.md"* ]]; then
  echo "memory-suggestion issue body does not include target memory file" >&2
  exit 1
fi

if [[ "${CREATED_ISSUES[1]}" != *"Future PRs must map each acceptance criterion"* ]]; then
  echo "memory-suggestion issue body does not include proposed memory text" >&2
  exit 1
fi

echo "review memory suggestions test passed"
