#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2025-2026 Marcus Quinn
# test-quality-sweep-comment-upsert.sh — quality sweep comment upsert guard.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || exit
SCRIPTS_DIR="${SCRIPT_DIR}/.."

readonly TEST_RED='\033[0;31m'
readonly TEST_GREEN='\033[0;32m'
readonly TEST_RESET='\033[0m'

TEST_ROOT=""
TESTS_RUN=0
TESTS_FAILED=0

print_result() {
	local test_name="$1"
	local passed="$2"
	local message="${3:-}"
	TESTS_RUN=$((TESTS_RUN + 1))
	if [[ "$passed" -eq 0 ]]; then
		printf '%bPASS%b %s\n' "$TEST_GREEN" "$TEST_RESET" "$test_name"
		return 0
	fi
	printf '%bFAIL%b %s\n' "$TEST_RED" "$TEST_RESET" "$test_name"
	if [[ -n "$message" ]]; then
		printf '       %s\n' "$message"
	fi
	TESTS_FAILED=$((TESTS_FAILED + 1))
	return 0
}

setup_test_env() {
	TEST_ROOT=$(mktemp -d)
	export TEST_ROOT
	mkdir -p "${TEST_ROOT}/bin" "${TEST_ROOT}/post"
	export PATH="${TEST_ROOT}/bin:${PATH}"
	export HOME="${TEST_ROOT}/home"
	export LOGFILE="${TEST_ROOT}/stats.log"
	mkdir -p "${HOME}/.aidevops/logs" "${HOME}/.config/aidevops"
	printf '{"initialized_repos":[]}\n' >"${HOME}/.config/aidevops/repos.json"
	create_gh_stub
	return 0
}

teardown_test_env() {
	if [[ -n "$TEST_ROOT" && -d "$TEST_ROOT" ]]; then
		rm -rf "$TEST_ROOT"
	fi
	return 0
}

create_gh_stub() {
	cat >"${TEST_ROOT}/bin/gh" <<'GHEOF'
#!/usr/bin/env bash
set -euo pipefail

if [[ "${1:-}" == "api" && " $* " == *"/issues/42/comments"* && " $* " == *"per_page=100"* ]]; then
	printf '%s\n' '[[{"id":987,"created_at":"2026-01-01T00:00:00Z","body":"## Daily Code Quality Sweep\n\n_Auto-generated by stats-wrapper.sh daily quality sweep._"}]]'
	exit 0
fi

if [[ "${1:-}" == "api" && "${2:-}" == *"/issues/comments/987"* && " $* " == *" --method PATCH "* ]]; then
	printf '%s\n' "$*" >"${TEST_ROOT}/post/patch.argv"
	exit 0
fi

printf 'unsupported gh invocation in quality upsert stub: %s\n' "$*" >&2
exit 1
GHEOF
	chmod +x "${TEST_ROOT}/bin/gh"
	return 0
}

source_stats_modules() {
	# shellcheck source=/dev/null
	source "${SCRIPTS_DIR}/stats-quality-sweep-tools.sh" 2>/dev/null
	return 0
}

test_build_sweep_comment_omits_repo_slug() {
	local body=""
	body=$(_build_sweep_comment "2026-01-01T00:00:00Z" "owner/private-repo" 1 "shell" "" "" "" "" "")
	if [[ "$body" == *"<!-- quality-sweep-latest -->"* && "$body" != *"owner/private-repo"* && "$body" == *"current repository"* ]]; then
		print_result "sweep comment marker present and repo slug omitted" 0
		return 0
	fi
	print_result "sweep comment marker present and repo slug omitted" 1 "Unexpected body: ${body}"
	return 0
}

test_upsert_edits_existing_comment() {
	local body="<!-- quality-sweep-latest -->
## Daily Code Quality Sweep

updated"
	if _upsert_quality_sweep_comment 42 owner/repo "$body"; then
		if [[ -f "${TEST_ROOT}/post/patch.argv" ]]; then
			print_result "quality sweep updates latest generated comment" 0
			return 0
		fi
		print_result "quality sweep updates latest generated comment" 1 "PATCH not captured"
		return 0
	fi
	print_result "quality sweep updates latest generated comment" 1 "upsert returned non-zero"
	return 0
}

main() {
	setup_test_env
	source_stats_modules
	test_build_sweep_comment_omits_repo_slug
	test_upsert_edits_existing_comment
	teardown_test_env

	printf '\nTests run: %d\n' "$TESTS_RUN"
	printf 'Failures: %d\n' "$TESTS_FAILED"
	if [[ "$TESTS_FAILED" -eq 0 ]]; then
		return 0
	fi
	return 1
}

main "$@"
