#!/usr/bin/env bash
# check-release-pr-scope.sh — fail closed when a release PR's declared scope
# drifts from the release currently derived from the integration branch head.
#
# Usage:
#   PR_TITLE="..." PR_BODY="..." HEAD_REF="develop" \
#     bash scripts/check-release-pr-scope.sh

set -euo pipefail

is_hotfix_branch() {
  case "${1:-}" in
    hotfix/*|fix/hotfix-*)
      return 0
      ;;
    *)
      return 1
      ;;
  esac
}

extract_declared_scope() {
  python3 - <<'PY'
import os
import re

title = os.environ.get("PR_TITLE", "")
body = os.environ.get("PR_BODY", "")
text = f"{title}\n{body}"
scope = r"(?:REQ-\d+|v\d+\.\d+\.\d+|v\d{4}\.\d{2}\.\d{2}(?:\.\d+)?)"

# An explicit release field is authoritative. Do not let a historical REQ in
# the surrounding prose override it, and fail closed if its value is invalid.
explicit_field = re.search(r"^[ \t\-*]*release:\s*(.*)$", text, re.IGNORECASE | re.MULTILINE)
if explicit_field:
    value = explicit_field.group(1).strip()
    match = re.match(rf"`?({scope})`?(?![A-Za-z0-9_.-])", value, re.IGNORECASE)
    if match:
        print(match.group(1))
    raise SystemExit(0)

patterns = [
    rf"\b({scope})\b",
]
for pattern in patterns:
    match = re.search(pattern, text, re.IGNORECASE)
    if match:
        print(match.group(1))
        break
PY
}

HEAD_REF="${HEAD_REF:-}"
BASE_REF="${BASE_REF:-}"
INTEGRATION_BRANCH="${INTEGRATION_BRANCH:-develop}"
RELEASE_BRANCH="${RELEASE_BRANCH:-main}"
if is_hotfix_branch "$HEAD_REF"; then
  echo "Hotfix PR detected (${HEAD_REF}) — release scope integrity check skipped."
  exit 0
fi

if [ ! -x scripts/derive-release-version.sh ]; then
  chmod +x scripts/derive-release-version.sh 2>/dev/null || true
fi
ALLOW_PENDING_TICKET_FALLBACK=0
if [ "$HEAD_REF" = "$INTEGRATION_BRANCH" ] && [ "$BASE_REF" = "$RELEASE_BRANCH" ]; then
  ALLOW_PENDING_TICKET_FALLBACK=1
fi
CURRENT_RELEASE=$(DEVAUDIT_ALLOW_PENDING_TICKET_FALLBACK="$ALLOW_PENDING_TICKET_FALLBACK" bash scripts/derive-release-version.sh)
if [ -z "$CURRENT_RELEASE" ]; then
  echo "::error::Could not derive the effective release scope from the current branch head."
  exit 1
fi

DECLARED_SCOPE=$(extract_declared_scope)
if [ -z "$DECLARED_SCOPE" ]; then
  echo "::error::Release Scope Integrity: the PR title/body do not declare a release scope."
  echo "::error::Derived effective scope: ${CURRENT_RELEASE}"
  echo "::error::Update the PR metadata or regenerate the release PR from the current integration head."
  exit 1
fi

if [ "$DECLARED_SCOPE" != "$CURRENT_RELEASE" ]; then
  echo "::error::Release Scope Integrity mismatch."
  echo "::error::Declared PR scope: ${DECLARED_SCOPE}"
  echo "::error::Derived effective scope: ${CURRENT_RELEASE}"
  echo "::error::Update the PR metadata or regenerate the release PR from the current integration head."
  exit 1
fi

if [[ "$CURRENT_RELEASE" =~ ^v[0-9]{4}\.[0-9]{2}\.[0-9]{2}(\.[0-9]+)?$ ]]; then
  DECLARATION="compliance/standalone-housekeeping/STANDALONE-HOUSEKEEPING-${CURRENT_RELEASE}.json"
  if ! printf '%s\n%s\n' "${PR_TITLE:-}" "${PR_BODY:-}" | grep -Fqi "Standalone housekeeping promotion"; then
    echo "::error::Bare-date releases may only be promoted with an explicit standalone housekeeping exception."
    echo "::error::Add 'Standalone housekeeping promotion' to the PR title or body and include ${DECLARATION}."
    exit 1
  fi
  chmod +x scripts/standalone-housekeeping-release.sh 2>/dev/null || true
  bash scripts/standalone-housekeeping-release.sh validate "$CURRENT_RELEASE" "$DECLARATION"
fi

if [[ "$CURRENT_RELEASE" =~ ^REQ-[0-9]+$ ]]; then
  if [ ! -x scripts/extract-release-metadata.sh ]; then
    chmod +x scripts/extract-release-metadata.sh 2>/dev/null || true
  fi
  # shellcheck disable=SC1091
  source scripts/extract-release-metadata.sh
  extract_release_metadata "$CURRENT_RELEASE"
  if printf '%s' "${RELEASE_SUMMARY:-}" | grep -q 'Bundled release context:'; then
    if ! printf '%s\n%s\n' "${PR_TITLE:-}" "${PR_BODY:-}" | grep -q "BUNDLED-CHANGES-${CURRENT_RELEASE}.md"; then
      echo "::error::Release Scope Integrity mismatch."
      echo "::error::Declared PR scope: ${DECLARED_SCOPE}"
      echo "::error::Derived effective scope: ${CURRENT_RELEASE} with bundled release context."
      echo "::error::PR metadata is missing the bundled release context marker for BUNDLED-CHANGES-${CURRENT_RELEASE}.md."
      echo "::error::Update the PR body or regenerate the release PR from the current integration head."
      exit 1
    fi
  fi
fi

echo "Release Scope Integrity verified for ${CURRENT_RELEASE}."
