#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2025-2026 Marcus Quinn
#
# Regression tests for worker-lifecycle body-quality escalation gates.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || exit
REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
LIFECYCLE_SCRIPT="${REPO_ROOT}/.agents/scripts/worker-lifecycle-common.sh"

TESTS_RUN=0
TESTS_FAILED=0
GH_COMMENT_COUNT=0

print_result() {
	local test_name="$1"
	local passed="$2"
	local message="${3:-}"

	TESTS_RUN=$((TESTS_RUN + 1))
	if [[ "$passed" -eq 0 ]]; then
		printf 'PASS %s\n' "$test_name"
		return 0
	fi

	printf 'FAIL %s\n' "$test_name"
	if [[ -n "$message" ]]; then
		printf '       %s\n' "$message"
	fi
	TESTS_FAILED=$((TESTS_FAILED + 1))
	return 0
}

gh_issue_comment() {
	local issue_number="$1"
	shift || true
	[[ -n "$issue_number" ]] || return 1
	GH_COMMENT_COUNT=$((GH_COMMENT_COUNT + 1))
	return 0
}

define_helpers_under_test() {
	local context_src=""
	local gate_src=""

	context_src=$(awk '
		/^_escalate_body_has_review_feedback_context\(\) \{/,/^}$/ { print }
	' "$LIFECYCLE_SCRIPT")
	gate_src=$(awk '
		/^_escalate_body_quality_gate\(\) \{/,/^}$/ { print }
	' "$LIFECYCLE_SCRIPT")

	if [[ -z "$context_src" || -z "$gate_src" ]]; then
		printf 'ERROR: could not extract lifecycle body-gate helpers\n' >&2
		return 1
	fi

	# shellcheck disable=SC1090  # dynamic source from extracted helper bodies
	eval "$context_src"
	# shellcheck disable=SC1090  # dynamic source from extracted helper bodies
	eval "$gate_src"
	return 0
}

test_review_feedback_body_bypasses_missing_context_gate() {
	local body=""
	GH_COMMENT_COUNT=0
	# shellcheck disable=SC2016  # literal body includes Markdown backticks and array syntax
	printf -v body '%s\n' \
		'## Unactioned Review Feedback' \
		'' \
		'**Source PR**: #2166' \
		'**File**: `general`' \
		'**Reviewers**: gemini' \
		'' \
		'### MEDIUM: gemini (gemini-code-assist[bot])' \
		'## Code Review' \
		'' \
		'This pull request correctly addresses an `unbound variable` error in `_run_generator` and the changes are well-implemented.' \
		'' \
		'_Auto-generated by `quality-feedback-helper.sh scan-merged`. Review each finding and either fix the code or dismiss with a reason._'

	if ! _escalate_body_quality_gate 3343 marcusquinn/aidevops 2 2 "$body"; then
		print_result "review-feedback context bypasses missing-context gate" 1 \
			"expected review-feedback body to allow escalation"
		return 0
	fi
	if [[ "$GH_COMMENT_COUNT" -ne 0 ]]; then
		print_result "review-feedback context bypasses missing-context gate" 1 \
			"expected no diagnostic comment, got ${GH_COMMENT_COUNT}"
		return 0
	fi
	print_result "review-feedback context bypasses missing-context gate" 0
	return 0
}

test_file_path_body_allows_escalation() {
	local body="## How
Files to Modify:
- EDIT: .agents/scripts/quality-feedback-findings-lib.sh:211-213"
	GH_COMMENT_COUNT=0

	if _escalate_body_quality_gate 99 marcusquinn/aidevops 2 2 "$body"; then
		print_result "file-path context allows escalation" 0
		return 0
	fi
	print_result "file-path context allows escalation" 1 \
		"expected file path body to pass gate"
	return 0
}

test_generic_vague_body_blocks_escalation() {
	local body="Please fix the quality issue."
	GH_COMMENT_COUNT=0

	if _escalate_body_quality_gate 100 marcusquinn/aidevops 2 2 "$body"; then
		print_result "generic vague body blocks escalation" 1 \
			"expected vague body to be blocked"
		return 0
	fi
	if [[ "$GH_COMMENT_COUNT" -ne 1 ]]; then
		print_result "generic vague body blocks escalation" 1 \
			"expected one diagnostic comment, got ${GH_COMMENT_COUNT}"
		return 0
	fi
	print_result "generic vague body blocks escalation" 0
	return 0
}

main() {
	if ! define_helpers_under_test; then
		return 1
	fi

	test_review_feedback_body_bypasses_missing_context_gate
	test_file_path_body_allows_escalation
	test_generic_vague_body_blocks_escalation

	printf '\nRan %s tests, %s failed.\n' "$TESTS_RUN" "$TESTS_FAILED"
	if [[ "$TESTS_FAILED" -gt 0 ]]; then
		return 1
	fi
	return 0
}

main "$@"
