#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2025-2026 Marcus Quinn
#
# test-requirements-lock-generated.sh — regression coverage for GH#25771
#
# The lock file is generated by pip freeze. SPDX/project header comments are
# allowed, but dependency-section annotations are maintained in requirements.txt
# so they are not lost when the lock file is regenerated.

set -euo pipefail

TEST_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || exit 1
REPO_ROOT="$(cd "${TEST_SCRIPT_DIR}/../../.." && pwd)" || exit 1
LOCK_FILE="${REPO_ROOT}/requirements-lock.txt"

main() {
	local line=""
	local seen_dependency=0
	local line_number=0

	if [[ ! -f "${LOCK_FILE:-}" ]]; then
		printf 'FAIL: requirements-lock.txt not found at %s\n' "${LOCK_FILE:-}" >&2
		return 1
	fi

	while IFS= read -r line || [[ -n "$line" ]]; do
		line_number=$((line_number + 1))

		if [[ "$seen_dependency" -eq 0 ]]; then
			if [[ "$line" == *==* && "$line" != \#* ]]; then
				seen_dependency=1
			fi
			continue
		fi

		if [[ "$line" == \#* ]]; then
			printf 'FAIL requirements-lock.txt contains dependency-section comment at line %s: %s\n' \
				"$line_number" "$line" >&2
			return 1
		fi
	done <"$LOCK_FILE"

	printf 'PASS requirements-lock.txt has no dependency-section comments\n'
	return 0
}

main "$@"
